Laravel Eloquent query with optional parameters

Jazzy

I am trying to learn whether or not there is a simple way to pass a variable number of parameters to a query in Eloquent, hopefully using an array.

From what I can find, there doesn't seem to be a way to do this without looping through the Input to see what was set in the request.

Examples here: Laravel Eloquent search two optional fields

This would work, but feels non-Laravel to me in its complexity/inelegance.

Here is where I am, and this may not be possible, just hoping someone else has solved a similar issue:

$where = array("user_id" => 123, "status" => 0, "something else" => "some value");

        $orders = Order::where($where)->get()->toArray();
        return Response::json(array(
                'orders' => $orders
                ),
            200
        );

That returns an error of course strtolower() expects parameter 1 to be string, array given.

Is this possible?

Plywood

Order::where actually returns an instance of query builder, so this is probably easier than you thought. If you just want to grab that instance of query builder and "build" your query one where() at a time you can get it like this:

$qb = (new Order)->newQuery();
foreach ($searchParams as $k => $v) {
    $qb->where($k, $v);
}
return $qb->get(); // <-- fetch your results

If you ever want to see what query builder is doing you can also execute that get() and shortly after:

dd(\DB::getQueryLog());

That will show you what the resulting query looks like; this can be very useful when playing with Eloquent.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Laravel Eloquent orWhere Query

分類Dev

Laravel eloquent query improvement

分類Dev

Laravel Eloquent query behaving strangely

分類Dev

Raw query to Eloquent. laravel

分類Dev

Laravel 4.2 Eloquent Dynamic Query

分類Dev

Regex for URL rewrite with optional query string parameters

分類Dev

Laravel join query using Laravel eloquent

分類Dev

In Laravel Eloquent why are some SQL parameters not bound in?

分類Dev

Laravel query multiple tables using eloquent

分類Dev

Laravel Eloquent query returning non object

分類Dev

Laravel Eloquent Repository - Query giving unexpected result?

分類Dev

Can't convert SQL query to laravel eloquent

分類Dev

Mysql Query Or Laravel Eloquent for Average of multiple column

分類Dev

Query relationship inside the relationship in Laravel Eloquent

分類Dev

Laravel 4.2 Eloquent using lists() with a join query

分類Dev

Laravel's Eloquent ORM versus Query Builder

分類Dev

Whats wrong with my Laravel eloquent query

分類Dev

passing optional query string parameters to http service call

分類Dev

Laravel - Eloquent converts query parameter to integer before comparison

分類Dev

How to switch code from db class to Eloquent query? Laravel

分類Dev

orWhere()内にANDを使用したLaravel Eloquent Query

分類Dev

Laravel Eloquent Query to Check Overlapping Start and End DateTime Fields

分類Dev

How can i convert this sql query in a eloquent laravel command?

分類Dev

Laravel: Alias not correctly written when using Haversine formula on Eloquent query

分類Dev

How to use query builder or raw SQL within a Laravel Eloquent relationship

分類Dev

Laravel Eloquent Query-count()の制限

分類Dev

pass optional parameters to require()

分類Dev

Optional templated parameters for a function

分類Dev

Using callbacks with optional parameters

Related 関連記事

  1. 1

    Laravel Eloquent orWhere Query

  2. 2

    Laravel eloquent query improvement

  3. 3

    Laravel Eloquent query behaving strangely

  4. 4

    Raw query to Eloquent. laravel

  5. 5

    Laravel 4.2 Eloquent Dynamic Query

  6. 6

    Regex for URL rewrite with optional query string parameters

  7. 7

    Laravel join query using Laravel eloquent

  8. 8

    In Laravel Eloquent why are some SQL parameters not bound in?

  9. 9

    Laravel query multiple tables using eloquent

  10. 10

    Laravel Eloquent query returning non object

  11. 11

    Laravel Eloquent Repository - Query giving unexpected result?

  12. 12

    Can't convert SQL query to laravel eloquent

  13. 13

    Mysql Query Or Laravel Eloquent for Average of multiple column

  14. 14

    Query relationship inside the relationship in Laravel Eloquent

  15. 15

    Laravel 4.2 Eloquent using lists() with a join query

  16. 16

    Laravel's Eloquent ORM versus Query Builder

  17. 17

    Whats wrong with my Laravel eloquent query

  18. 18

    passing optional query string parameters to http service call

  19. 19

    Laravel - Eloquent converts query parameter to integer before comparison

  20. 20

    How to switch code from db class to Eloquent query? Laravel

  21. 21

    orWhere()内にANDを使用したLaravel Eloquent Query

  22. 22

    Laravel Eloquent Query to Check Overlapping Start and End DateTime Fields

  23. 23

    How can i convert this sql query in a eloquent laravel command?

  24. 24

    Laravel: Alias not correctly written when using Haversine formula on Eloquent query

  25. 25

    How to use query builder or raw SQL within a Laravel Eloquent relationship

  26. 26

    Laravel Eloquent Query-count()の制限

  27. 27

    pass optional parameters to require()

  28. 28

    Optional templated parameters for a function

  29. 29

    Using callbacks with optional parameters

ホットタグ

アーカイブ