从两个不同的表获取数据的Laravel视图错误

时间:2018-08-30 07:52:02

标签: html laravel

用于从数据库收集的数据的HTML视图刀片

无法从另一个我不知道为什么的表中获取数据

@if(count($shift))
    @foreach($shift as $data)
    <tr>
    <td>{{$data->serial}}</td>
        <td>{{$data->date}}</td>
        <td>{{$data->startshift}}</td>
        <td>{{$data->endshift}}</td>
    @foreach($tracking as $track)
      <!--here is the problem-->
    @endforeach 
    @endforeach 
    </tr>

用于连接数据库和视图的控制器

    $shift = DB::table('employee_schedule')
  ->select('id','user_id as user_id','serial as serial','date as date','startshift as startshift','endshift as endshift')
  ->orderBy('id','ASC')
  ->paginate(5);

$tracking = DB::table('empsheduling')
->select('serial as serial','date as date','startshift as startshift','endshift as endshift','Hourswork as Hourswork')
->first();

return view('shifTracker.viewshifTracker',compact('shift','tracking'));

2 个答案:

答案 0 :(得分:0)

您已经选择了可实现表的第一条记录,请查看查询的结尾:first()

first()替换为get(),或者在您的视图中删除循环并使用$tracking->data获取数据。

答案 1 :(得分:0)

我已经通过将first()更改为get()找到了解决方案。谢谢