0
$demos = Demo::whereIn('store_id', [1,2,4], function($query){
                    $query->where('status', 1);
        })->paginate(10);

I know that this thats not working, But how can I work with this logic..?

[Select * from 'demos' where store_id in 1,2,4 and status = 1 ]

2 Answers 2

2

If I understand correctly you need something like this.

$demos = Demo::whereIn('store_id', [1, 2, 4])->where('status', 1)->paginate(10);

Chained "where" Eloquent methods == "AND" database query conditions.

Sign up to request clarification or add additional context in comments.

Comments

-1

Sometimes, it is better not use ORM`ish approach.
They say, old plain SQL is your best friend. So.

$data = \DB::select(
  \DB::raw("SELECT * FROM `demos` WHERE `store_id` IN (:id1, :id2, :id3)"), array( ':id1' => 1, ':id2' => 2, ':id3' => 3 )
);

Or, if you have unknown count of store_id entries, you could:

$ids = array(1, 2, 3, 4, ...); // looks like unknown ammount of entries
$data = \DB::select(
  \DB::raw("SELECT * FROM `demos` WHERE `store_id` IN (".rtrim(str_repeat('?,', count($ids)),',').")"), $ids
);

2 Comments

There are time's where it's better to not use an ORM but this is not one of them. Eloquent is perfectly suited for this query. Your second example is much harder to read than the same thing in Eloquent
Maybe, but with plain SQL approach you always can write down any query you want, without the need to search the documentation for the desired method or function of your favorite ORM.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.