Home> PHP Framework> Laravel> body text

laravel query writes multiple conditions

王林
Release: 2023-05-20 13:21:08
Original
1127 people have browsed it

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>

  1. basic query< li>< ol>

    when querying in can basic methods, such as 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>
    copy after login< div>< div>

    above code> method specify two the first< is used obtain a single record. if result empty, null< returned. p>

    1. advanced query< ol>

      the model also provides many advanced handle more complex queries. are some commonly methods: p>

      2.1 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>

      2.2 code>method< p>

      orwhere< code>the if any one of met. or 2: ->where('category_id', ->orwhere('status', div>

      2.3 wherehas< p>

      wherehas< records associated that meet filter criteria. all comments: ->wherehas('comments') div>

      in 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>

      2.4 p>

      wherein< allow follow fields filter results by value 2, 3: ->wherein('status', [1, 3]) div>

      1. mixed use< ol>

        in actual applications, combination complete category number greater than 5: }, '>', 5) to >< symbol whether 5.

        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!

        in>
    source:php.cn
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!