<small id='wrG2L'></small><noframes id='wrG2L'>

        <tfoot id='wrG2L'></tfoot>
      1. <i id='wrG2L'><tr id='wrG2L'><dt id='wrG2L'><q id='wrG2L'><span id='wrG2L'><b id='wrG2L'><form id='wrG2L'><ins id='wrG2L'></ins><ul id='wrG2L'></ul><sub id='wrG2L'></sub></form><legend id='wrG2L'></legend><bdo id='wrG2L'><pre id='wrG2L'><center id='wrG2L'></center></pre></bdo></b><th id='wrG2L'></th></span></q></dt></tr></i><div id='wrG2L'><tfoot id='wrG2L'></tfoot><dl id='wrG2L'><fieldset id='wrG2L'></fieldset></dl></div>

          <bdo id='wrG2L'></bdo><ul id='wrG2L'></ul>
        <legend id='wrG2L'><style id='wrG2L'><dir id='wrG2L'><q id='wrG2L'></q></dir></style></legend>
      2. 查询关系 Eloquent

        时间:2023-09-24

              <tbody id='lYAKN'></tbody>
              <i id='lYAKN'><tr id='lYAKN'><dt id='lYAKN'><q id='lYAKN'><span id='lYAKN'><b id='lYAKN'><form id='lYAKN'><ins id='lYAKN'></ins><ul id='lYAKN'></ul><sub id='lYAKN'></sub></form><legend id='lYAKN'></legend><bdo id='lYAKN'><pre id='lYAKN'><center id='lYAKN'></center></pre></bdo></b><th id='lYAKN'></th></span></q></dt></tr></i><div id='lYAKN'><tfoot id='lYAKN'></tfoot><dl id='lYAKN'><fieldset id='lYAKN'></fieldset></dl></div>

              <small id='lYAKN'></small><noframes id='lYAKN'>

                  <bdo id='lYAKN'></bdo><ul id='lYAKN'></ul>
                  <tfoot id='lYAKN'></tfoot>
                • <legend id='lYAKN'><style id='lYAKN'><dir id='lYAKN'><q id='lYAKN'></q></dir></style></legend>
                • 本文介绍了查询关系 Eloquent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有 News 模型,而且 News 有很多评论,所以我在 News 模型中做了这个:

                  I have News model, and News has many comments, so I did this in News model:

                  public function comments(){
                      $this->hasMany('Comment', 'news_id');
                  }
                  

                  但是我在 comments 表中也有字段 trashed,我只想选择没有被删除的评论.所以 废弃了 <>1.所以我想知道有没有办法做这样的事情:

                  But I also have field trashed in comments table, and I only want to select comments that are not trashed. So trashed <> 1. So I wonder is there a way to do something like this:

                  $news = News::find(123);
                  $news->comments->where('trashed', '<>', 1); //some sort of pseudo-code
                  

                  有没有办法使用上述方法,或者我应该写这样的东西:

                  Is there a way to use above method or should I just write something like this:

                  $comments = Comment::where('trashed', '<>', 1)
                      ->where('news_id', '=', $news->id)
                      ->get();
                  

                  推荐答案

                  这些都适合你,选择你最喜欢的:

                  Any of these should work for you, pick the one you like the most:

                  1. 急切加载.

                  1. Eager-loading.

                  $comments = News::find(123)->with(['comments' => function ($query) {
                      $query->where('trashed', '<>', 1);
                  }])->get();
                  

                  您可以通过 use($param) 方法将参数注入查询函数,这允许您在运行时使用动态查询值.

                  You can inject the parameter to query function by use($param) method, that allows you to use dynemic query value at runtime.

                  延迟加载

                  $news = News::find(123);
                  $comments = $news->comments()->where('trashed', '<>', 1)->get();
                  

                  <小时>

                  不过,我忍不住注意到,您可能想做的是处理软删除,而 Laravel 有内置功能可以帮助您:http://laravel.com/docs/eloquent#soft-deleting

                  这篇关于查询关系 Eloquent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何在 Laravel 中使用 Eloquent 最后对 NULL 值进行排 下一篇:如何在 Eloquent ORM laravel 中获取最后一个插入 ID

                  相关文章

                  最新文章

                  <i id='VHGuR'><tr id='VHGuR'><dt id='VHGuR'><q id='VHGuR'><span id='VHGuR'><b id='VHGuR'><form id='VHGuR'><ins id='VHGuR'></ins><ul id='VHGuR'></ul><sub id='VHGuR'></sub></form><legend id='VHGuR'></legend><bdo id='VHGuR'><pre id='VHGuR'><center id='VHGuR'></center></pre></bdo></b><th id='VHGuR'></th></span></q></dt></tr></i><div id='VHGuR'><tfoot id='VHGuR'></tfoot><dl id='VHGuR'><fieldset id='VHGuR'></fieldset></dl></div>

                  <small id='VHGuR'></small><noframes id='VHGuR'>

                    • <bdo id='VHGuR'></bdo><ul id='VHGuR'></ul>

                  1. <tfoot id='VHGuR'></tfoot>

                      <legend id='VHGuR'><style id='VHGuR'><dir id='VHGuR'><q id='VHGuR'></q></dir></style></legend>