in laravel, we often need to query based on multiple conditions. these conditions may include field values, time ranges, relationships, and more. in this article, will discuss how use laravel eloquent for multi-condition query. < p>
when querying in can basic methods, such as above code> method specify two the the model also provides many advanced handle more complex queries. are some commonly methods: p> 2.1 2.2 code>method< p> 2.3 in 2.4 p> in actual applications, combination complete category number greater than 5: }, '>', 5) to conclusion< in perform using easily operations. mastering skills help tasks efficiently during development. p>
The above is the detailed content of laravel query writes multiple conditions. For more information, please follow other related articles on the PHP Chinese website!
where< code>,
orwhere<
wherein<
wherenotin< etc. methods be chained combine for example, the following code articles table with id 1 status 1: p>
$article="DB::table('articles')" ->where('id', 1) ->where('status', ->first();< pre>
first< is used obtain a single record. if result empty,
null< returned. p>
wherebetween< p>
wherebetween< allows us records within specified range. created between 2019 2020: >$articles="DB::table('articles')" ->wherebetween('created_at', ['2019-01-01', '2020-12-31']) ->get();< div>
orwhere< code>the if any one of met. or 2: ->where('category_id', ->orwhere('status', div>
wherehas< p>
wherehas< records associated that meet filter criteria. all comments: ->wherehas('comments') div>
comments< whether there return article want further comments certain conditions, pass additional filtering method, example: ->wherehas('comments', function($query) { $query->where('status', 1); }) have passes only queries 1, returns them. p>
wherein< allow follow fields filter results by value 2, 3: ->wherein('status', [1, 3]) div>
>< symbol whether 5.