dongzhi5386 2014-04-19 16:13
浏览 28
已采纳

Laravel有许多关系没有循环显示结果

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 forloop doesn't work!

Within my show view, I have:

@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 forloop doesn't work when there is less 2 results, and why it only loops through the first result. Thank you.

  • 写回答

1条回答 默认 最新

  • dongtang1944 2014-04-19 16:38
    关注

    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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算