Laravel预测了多个数据

时间:2018-05-28 18:38:57

标签: php laravel foreach

这就是我希望的样子 enter image description here 实际 enter image description here 状态被打印3次

我从控制器返回这些对象:

return view('ViewTicket') ->with('tickets', $tickets)
        ->with('user', $user)
        ->with('priority', $priority)
        ->with('status', $status)
        ->with('type', $type);

但是我想在我的视图中打印相应的字段:

   @foreach ($tickets as $t)
   <tr> 
    <td> {{$t->id}} </td>
    @foreach ($user as $u)

     @if($t->user_id==$u->Id)
    <td>{{ $u->UserName }}</td>
  @endif
    @endforeach

即便这样也无法解决我的问题。有没有办法避免循环中的循环来获取这些数据?目标是获取每张票的相应字段

如果我dd($user)它会返回3个值 当我在我的视图中循环时,它会显示9个值,这意味着它循环了3次

先谢谢

1 个答案:

答案 0 :(得分:-1)

ASSUMPTIONS

  • $tickets is a collection of ticket objects.
  • $user is a collection of user objects.

CODE

@foreach ($tickets as $t)
   <tr> 
    @foreach ($user as $u)
     @if($t->user_id == $u->Id)
        <td> {{$t->id}} </td>
        <td>{{ $u->UserName }}</td>
     @endif
    @endforeach
   </tr>
@endforeach