• <tfoot id='PCymM'></tfoot>

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

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

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

        Laravel (HasMany) 不检索值

        时间:2023-09-22

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

              2. <legend id='tFPdQ'><style id='tFPdQ'><dir id='tFPdQ'><q id='tFPdQ'></q></dir></style></legend>

                  <tbody id='tFPdQ'></tbody>

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

                  本文介绍了Laravel (HasMany) 不检索值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有以下型号:

                      namespace App;
                  
                  use IlluminateDatabaseEloquentModel;
                  
                  class forum_category extends Model
                  {
                      //
                  
                      protected $table = 'forum_category';
                  
                      public function posts()
                      {
                          $this->hasMany('Appforum_post');
                      }
                  }
                  

                  namespace App;
                  
                  use IlluminateDatabaseEloquentModel;
                  
                  class forum_post extends Model
                  {
                      //
                      protected $table = 'forum_post';
                  
                  
                      public function category()
                      {
                          $this->belongsTo('Appforum_category');
                      }
                  }
                  

                  在我的控制器中,我尝试使用他们的帖子获取所有类别:

                  in my controller i attempt to get all categories with their posts:

                      namespace AppHttpControllers;
                  
                  use IlluminateHttpRequest;
                  use IlluminateRoutingController;
                  
                  use Appforum_category as forum_category;
                  
                  
                  class ForumController extends Controller
                  {
                  
                      public function __construct()
                      {
                  
                      }
                  
                      public function index ()
                      {
                  
                          $categories = forum_category::all();
                  
                          return view('forum', ['categories' => $categories]);
                      }
                  
                      public function createPost()
                      {
                  
                      }
                  
                      public function createReply()
                      {
                  
                      }
                  }
                  

                  但是它似乎只返回我的类别.

                  However it seems to only be returning my categories.

                  谁能告诉我我做错了什么?

                  Can someone tell me what ive done wrong?

                  推荐答案

                  查询应如下所示:

                  $categories = forum_category::with('posts')->get();
                  

                  https://laravel.com/docs/5.5/eloquent-relationships#eager-loading

                  如果您在 forum_post 表中有 category_id,这将加载具有相关帖子的类别.

                  If you have category_id in the forum_post table, this will load categories with related posts.

                  然后只需遍历集合以获取帖子:

                  Then just iterate over the collection to get posts:

                  @foreach ($categories as $category)
                      @foreach ($category->posts as $post)
                          {{ $post->title }}
                      @endforeach
                  @endforeach
                  

                  另外,关系应该是:

                  public function category()
                  {
                      return $this->belongsTo('Appforum_category');
                  }
                  

                  这篇关于Laravel (HasMany) 不检索值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Laravel:让 Eloquent 创建嵌套 SELECT 的正确方法 下一篇:Laravel 使用 Sum 和 Groupby

                  相关文章

                  最新文章

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

                  1. <legend id='HVZJn'><style id='HVZJn'><dir id='HVZJn'><q id='HVZJn'></q></dir></style></legend>

                    <tfoot id='HVZJn'></tfoot>