Laravel hasMany 关系不循环显示结果

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23172367/
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 09:23:29  来源:igfitidea点击:

Laravel hasMany relationship not looping through to display results

phpfor-looplaravellaravel-4

提问by Jarek Tkaczyk

An alert can have many messages associated with it, through a foreign key. Each message sent is also attached to a user, through a foreign key. On viewing the alert, if such messages exist (they are not required), I want to display each message, along with the associated user's details.

通过外键,警报可以有许多与之相关的消息。发送的每条消息也通过外键附加到用户。在查看警报时,如果存在此类消息(它们不是必需的),我想显示每条消息以及相关用户的详细信息。

User model:

用户模型:

public function alerts()
{
    return $this->hasMany('Alert');
}

public function messages()
{
    return $this->hasMany('Message');
}

Alert model:

警报模型:

public function user()
    {
        return $this->belongsTo('User');
    }

    public function messages()
    {
        return $this->hasMany('Message');
    }

I have noticed if the alert doesn't have any messages associated with it, the forloopdoesn't work!

我注意到如果警报没有任何与之关联的消息,则forloop不起作用!

Within my showview, I have:

在我show看来,我有:

@foreach($alerts as $alert)
    <tr>
        <td>{{ $alerts->messages->first()->firstname }}</td>
        <td>{{ $alerts->messages->first()->user->email }}</td>
        <td>{{ $alerts->messages->first()->user->phone_number }}</td>
        <td>{{ $alerts->messages->first()->message }}</td>
        <td>{{ date("j F Y", strtotime($alerts->messages->first()->created_at)) }}</td>
        <td>{{ date("g:ia", strtotime($alerts->messages->first()->created_at)) }}</td>
    </tr>
@endforeach 

Which works great, if there are messages to show, but it only loops through the first message, not the rest of them. The controller pulling in the data is:

如果有消息要显示,这很好用,但它只循环显示第一条消息,而不是其余消息。拉入数据的控制器是:

public function show($id)
    {
        $alert = Alert::where('id','=',$id)->first();
        $this->layout->content = View::make('agents.alert.show', 
            array('alerts' => $alert));
    }

Any guidance as to why the forloopdoesn't work when there is less 2 results, and why it only loops through the first result. Thank you.

关于为什么在forloop少于 2 个结果时不起作用的任何指导,以及为什么它只循环遍历第一个结果。谢谢你。

回答by Jarek Tkaczyk

First I suggest using eager loading for related models or you will run many db queries that you don't want nor need:

首先,我建议对相关模型使用预先加载,否则您将运行许多您不需要也不需要的数据库查询:

public function show($id)
{
    $alert = Alert::with('messages.user')->where('id','=',$id)->first();
    $this->layout->content = View::make('agents.alert.show', array('alert' => $alert));
}

Then in you view spin through messages, not alerts as you don't have many of them:

然后在您查看旋转消息,而不是警报,因为您没有很多消息:

@foreach($alert->messages as $message)
<tr>
    <td>{{ $message->firstname }}</td>
    // if you are sure there is a user for each message, otherwise you need a check for null on $message->user
    <td>{{ $message->user->email }}</td>
    <td>{{ $message->user->phone_number }}</td> 
    <td>{{ $message->message }}</td>
    <td>{{ date("j F Y", strtotime($message->created_at)) }}</td>
    <td>{{ date("g:ia", strtotime($message->created_at)) }}</td>
</tr>
@endforeach