How to write multiple where conditions in single where condition in laravel?

0

Issue

I am using multiple where conditions in my query it’s working fine but i need to refactor the code is there any way to write both conditions in one where condition..

DB::table('books')->where('user_id',$uID)->where('author',$author_name)->delete();

Solution

Pass array of conditions to the where function.

DB::table('books')->where([['user_id','=',$uID],['author','=',$author_name]])->delete();

As per Doc :

pass an array of conditions to the where function. Each element of the
array should be an array containing the three arguments typically
passed to the where method

Ref:https://laravel.com/docs/8.x/queries#where-clauses

Answered By – John Lobo

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More