Ric*_*ore 3 foreach relationship laravel
堆栈溢出,
我想要完成的事情:
我想循环通过数据库找到所有(英雄)与他们的相关(访谈).我还希望每次采访都与它相关(故事)和(图像).到目前为止,我可以dd($英雄),我可以看到阵列正确地抓住每个英雄的面试,每次采访都有相关的图像和故事.如何正确循环?
错误
Invalid argument supplied for foreach() (View: /Users/plastics1509moore/Desktop/elephant_gin/resources/views/administration/index.blade.php)
Run Code Online (Sandbox Code Playgroud)
这就是我所做的:
控制器:
$heroes = Hero::with('Interview', 'Interview.stories', 'Interview.images')->orderBy('position', 'asc')->get();
Run Code Online (Sandbox Code Playgroud)
模型关系:
英雄:
public function Interview()
{
return $this->hasOne('App\Interview', 'heroInt_id');
}
Run Code Online (Sandbox Code Playgroud)
专访:
public function relationships()
{
return $this->belongsTo('App\Hero');
}
public function stories()
{
return $this->hasMany('App\InterviewStory');
}
public function images()
{
return $this->hasMany('App\InterviewImage');
}
Run Code Online (Sandbox Code Playgroud)
InterviewImage:
public function relationships()
{
return $this->belongsTo('App\Interview');
}
Run Code Online (Sandbox Code Playgroud)
InterviewStory
public function relationships()
{
return $this->belongsTo('App\Interview');
}
Run Code Online (Sandbox Code Playgroud)
HTML:
环:
@foreach($heroes as $hero)
@foreach($hero->Interview as $story)
<div>{{$story->id}}</div>
@endforeach
@endforeach
Run Code Online (Sandbox Code Playgroud)
你不能循环采访,因为这是一种hasOne关系.你可能想做的是
@foreach($heroes as $hero)
@foreach($hero->interview->stories as $story)
<div>{{$story->id}}</div>
@endforeach
@endforeach
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8560 次 |
| 最近记录: |