循环关系 Laravel

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33919113/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:44:36  来源:igfitidea点击:

Loop through relationships Laravel

laravelforeachrelationship

提问by Ricki Moore

Stackoverflow,

堆栈溢出,

What I want to accomplish:

我想要完成的事情:

I want to Loop through the database to find all the (Heroes) with their related (Interview). I want to also pull each Interview with it's related (Story) and (Image). So far I can dd($heroes) and I can see the array correctly grab each hero with their interview and each interview has it's associated image and story. How do I loop through this correctly?

我想遍历数据库以找到所有(英雄)及其相关(采访)。我还想把每次采访都与它相关的(故事)和(图像)拉出来。到目前为止,我可以 dd($heroes) 并且我可以看到数组在他们的采访中正确地抓住了每个英雄,并且每次采访都有相关的图像和故事。我如何正确循环这个?

Error

错误

Invalid argument supplied for foreach() (View: /Users/plastics1509moore/Desktop/elephant_gin/resources/views/administration/index.blade.php)

This is what I have done:

这就是我所做的:

Controller:

控制器:

$heroes = Hero::with('Interview', 'Interview.stories', 'Interview.images')->orderBy('position', 'asc')->get();

Model Relationships:

模型关系:

Hero:

英雄:

public function Interview()
{
    return $this->hasOne('App\Interview', 'heroInt_id');
}

Interview:

面试:

    public function relationships()
{
    return $this->belongsTo('App\Hero');
}
public function stories()
{
    return $this->hasMany('App\InterviewStory');
}
public function images()
{
    return $this->hasMany('App\InterviewImage');
}

InterviewImage:

采访图片:

 public function relationships()
{
    return $this->belongsTo('App\Interview');
}

InterviewStory

采访故事

public function relationships()
{
    return $this->belongsTo('App\Interview');
}

Html:

网址:

Loop:

环形:

    @foreach($heroes as $hero)
       @foreach($hero->Interview as $story)
         <div>{{$story->id}}</div>
        @endforeach    
    @endforeach

回答by PeterPan666

You can't loop on Interview because this a hasOnerelationship. What you might want to do is

你不能循环采访,因为这是一种hasOne关系。你可能想要做的是

@foreach($heroes as $hero)
   @foreach($hero->interview->stories as $story)
     <div>{{$story->id}}</div>
    @endforeach    
@endforeach

回答by Santiago Mendoza Ramirez

I think your problem is because not following laravel "assumes". In documentation:

我认为你的问题是因为没有遵循laravel“假设”。在文档中:

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');

此外,Eloquent 假设外键的值应该与父键的 id 列匹配。换句话说,Eloquent 将在 Phone 记录的 user_id 列中查找用户 id 列的值。如果您希望关系使用 id 以外的值,您可以将第三个参数传递给 hasOne 方法,指定您的自定义键:

return $this->hasOne('App\Phone', 'foreign_key', 'local_key');

What is the id of Hero? If it's different than id, then you have to add the local_keyname.

英雄的id是什么?如果它不同于id,那么您必须添加local_key名称。