遍历关系 Laravel
Loop through relationships Laravel
计算器,
我想完成的事情:
我想循环遍历数据库以查找所有(英雄)及其相关(采访)。我还想将每个采访都与其相关的(故事)和(图像)拉出来。到目前为止,我可以 dd($heroes) 并且我可以看到数组正确地抓住了每个英雄的采访,并且每个采访都有相关的图像和故事。我该如何正确循环?
错误
Invalid argument supplied for foreach() (View: /Users/plastics1509moore/Desktop/elephant_gin/resources/views/administration/index.blade.php)
这是我所做的:
控制器:
$heroes = Hero::with('Interview', 'Interview.stories', 'Interview.images')->orderBy('position', 'asc')->get();
模型关系:
英雄:
public function Interview()
{
return $this->hasOne('App\Interview', 'heroInt_id');
}
采访:
public function relationships()
{
return $this->belongsTo('App\Hero');
}
public function stories()
{
return $this->hasMany('App\InterviewStory');
}
public function images()
{
return $this->hasMany('App\InterviewImage');
}
采访图片:
public function relationships()
{
return $this->belongsTo('App\Interview');
}
采访故事
public function relationships()
{
return $this->belongsTo('App\Interview');
}
Html:
循环:
@foreach($heroes as $hero)
@foreach($hero->Interview as $story)
<div>{{$story->id}}</div>
@endforeach
@endforeach
我认为你的问题是因为没有关注 laravel "assumes"。在文档中:
Additionally, Eloquent assumes that the foreign key should have a
value matching the id column of the parent. In other words, Eloquent
will look for the value of the user's id column in the user_id column
of the Phone record. If you would like the relationship to use a value
other than id, you may pass a third argument to the hasOne method
specifying your custom key:
return $this->hasOne('App\Phone', 'foreign_key', 'local_key');
Hero的id是多少?如果它不同于 id
,那么您必须添加 local_key
名称。
您不能循环采访,因为这是 hasOne
关系。你可能想要做的是
@foreach($heroes as $hero)
@foreach($hero->interview->stories as $story)
<div>{{$story->id}}</div>
@endforeach
@endforeach
计算器,
我想完成的事情:
我想循环遍历数据库以查找所有(英雄)及其相关(采访)。我还想将每个采访都与其相关的(故事)和(图像)拉出来。到目前为止,我可以 dd($heroes) 并且我可以看到数组正确地抓住了每个英雄的采访,并且每个采访都有相关的图像和故事。我该如何正确循环?
错误
Invalid argument supplied for foreach() (View: /Users/plastics1509moore/Desktop/elephant_gin/resources/views/administration/index.blade.php)
这是我所做的:
控制器:
$heroes = Hero::with('Interview', 'Interview.stories', 'Interview.images')->orderBy('position', 'asc')->get();
模型关系:
英雄:
public function Interview()
{
return $this->hasOne('App\Interview', 'heroInt_id');
}
采访:
public function relationships()
{
return $this->belongsTo('App\Hero');
}
public function stories()
{
return $this->hasMany('App\InterviewStory');
}
public function images()
{
return $this->hasMany('App\InterviewImage');
}
采访图片:
public function relationships()
{
return $this->belongsTo('App\Interview');
}
采访故事
public function relationships()
{
return $this->belongsTo('App\Interview');
}
Html:
循环:
@foreach($heroes as $hero)
@foreach($hero->Interview as $story)
<div>{{$story->id}}</div>
@endforeach
@endforeach
我认为你的问题是因为没有关注 laravel "assumes"。在文档中:
Additionally, Eloquent assumes that the foreign key should have a value matching the id column of the parent. In other words, Eloquent will look for the value of the user's id column in the user_id column of the Phone record. If you would like the relationship to use a value other than id, you may pass a third argument to the hasOne method specifying your custom key:
return $this->hasOne('App\Phone', 'foreign_key', 'local_key');
Hero的id是多少?如果它不同于 id
,那么您必须添加 local_key
名称。
您不能循环采访,因为这是 hasOne
关系。你可能想要做的是
@foreach($heroes as $hero)
@foreach($hero->interview->stories as $story)
<div>{{$story->id}}</div>
@endforeach
@endforeach