goslscs
goslscs

Reputation: 197

Laravel Eloquent Query Using WHERE with AND orWhere

I am beginner in Laravel. In use in my project Laravel 5.8. I have this array:

array:4 [
  0 => 1
  1 => 3
  2 => 4
  3 => 5
]

and function:

public function getPaymentMethods(array $paymentId)
    {
            dd($paymentId);
            return PaymentSettings::where('id', '=', $paymentId)->get();
    }

It's not working because in $paymentId I have array. How can I change my code function to use with array (many where/orWhere)?

I need something like this (but in laravel eloquent):

SELECT * from payment_settings where id = 1 or id = 3 or id = 4 or id = 5.

Upvotes: 0

Views: 150

Answers (2)

Adesh Khanna
Adesh Khanna

Reputation: 262

where clause is not used to searche from array, whereIn is used. Usage -

return PaymentSettings::whereIn('id', $paymentId)->get();

Upvotes: 0

Rahul
Rahul

Reputation: 18577

You need whereIn,

return PaymentSettings::whereIn('id', $paymentId)->get();

Note: The whereIn method verifies that a given column's value is contained within the given array:

You can see detailed documentation here.

Upvotes: 1

Related Questions