duangang4940
2016-02-28 03:27
浏览 199
已采纳

Laravel嵌套关系foreach循环

I have this relationship betwen item and episode

item:

public function episodes()
    {
        return $this->hasMany(episode::class);
    }

episodes:

public function item()
        {
            return $this->belongsTo(item::class);
        }

In controller

$latestanimes = DB::table('episodes')->where('category', 'anime')
            ->orderBy('created_at', 'desc')
            ->take(5)
            ->get();

What I'm trying to do is get the title of the anime it belongs to of each episode

                       @foreach($latestanimes as $episode)
                              <tr>
                                <td>
                                    @foreach($episode->item as $parrent)
                                      {{  $parrent->title; }}
                                    @endforeach
                                </td>
                                <td>{{ $episode->number }}</td>
                                <td>{{ $episode->category }}</td>
                            </tr>
                        @endforeach

This gives me "Undefined property: stdClass::$item"

I tried debugging in routes.php

Route::get('/dd/{id}', function($id){

        $episode=App\episode::find($id);
        echo $episode->name. '<hr>';

        $item=$episode->item;
        echo $item->title;

    });

And this works ... Is there a way to access it directly? like

{{ $episode->item->title }}

Thanks in advance

图片转代码服务由CSDN问答提供 功能建议

我在项目和剧集之间有关系 < p> item:

  public function episodes()
 {
 return $ this-&gt; hasMany(episode :: class); 
} 
   
 
 

剧集:

 公共功能项目()
 {
返回$ this-&gt; belongsTo(item :: class)  ); 
} 
   
 
 

在控制器中

  $ latestanimes = DB :: table('episodes')  - &gt; where('category','anime')
  - &gt; orderBy('created_at','desc')
  - &gt; take(5)
  - &gt; get(); 
   
 
 

我要做的是获取每集的动画片名称

  @foreach(  $ latestanimes as $ episode)
&lt; tr&gt; 
&lt; td&gt; 
 @foreach($ episode-&gt; item as $ parrent)
 {{$ pa  rrent-&GT;标题;  }} 
 @endforeach 
&lt; / td&gt; 
&lt; td&gt; {{$ episode-&gt; number}}&lt; / td&gt; 
&lt; td&gt; {{$ episode-&gt; category}}  &lt; / td&gt; 
&lt; / tr&gt; 
 @endforeach 
   
 
 

这给了我“Undefined property:stdClass :: $ item”

我尝试在routes.php中调试

  Route :: get('/ dd / {id}',function($ id){
  
 $ episode = App \ episode :: find($ id); 
 echo $ episode-&gt; name。'&lt; hr&gt;'; 
 
 $ item = $ episode-&gt; item; 
 echo  $ item-&gt; title; 
 
}); 
   
 
 

这有效...... 有没有办法直接访问它? 喜欢

  {{$ episode-&gt; item-&gt; title}} 
   
 
 

谢谢 提前

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douba05167 2016-02-28 03:52
    已采纳

    Fixed it. I was using query builder directly with DB which is not eloquent.

      $latestanimes = episode::with('item')  //eager loading, thanks lagbox
                ->where('category', 'anime')
                ->orderBy('created_at', 'desc')
                ->take(5)
                ->get();
    

    and I can access it directly how I wanted in the first place

    {{$episode->item->title}}
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • douhuocuo9012 2016-02-28 03:37

    some problem in your code that is episodes model you are writing belongsTo that means a episodes has only one item that cause foreach loop does't work .

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题