0

I have a table customer in my database:

Customer Id_customer name surname ...

And an array of id_customers:

$c = array[2,4,8,9]

I can obtain the customers like this:

$customers = Customer::all();

But, how can I obtain in Laravel just the customer contained in the array?

 $customers = Customer::all()->where(?¿?¿);

Very thanks in advance

2
  • Customer::whereIn('Id_customer',$c)->get() Commented Oct 18, 2018 at 13:42
  • This would be correct? Customer::whereIn('Id_customer',$request)->get(). Where $request is an array of id's Commented Oct 18, 2018 at 13:52

1 Answer 1

4

Try using the whereIn() method.

$users = Customer::whereIn('Id_customer', [1, 2, 3])
             ->get();

Read the docs on this here: https://laravel.com/docs/5.7/queries

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

4 Comments

Can I change [1,2,3] for $request (Being $request an array of id customers)?
Yes you can, you can replace it with whatever as long as it is an array of integers (you can use $c from your original question too). Although are you sure this $request variable is really an array?
Just its s example. I will use $customers. Where $customers = $request->all(); Very thanks!
Be aware your $request->all() is probably an array containing your customer_id array. see laravel.com/docs/5.7/requests#retrieving-input

Your Answer

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