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

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题