从两个不同的表中获取数据时,Laravel查看错误

问题描述 投票:0回答:2

用于从数据库收集的数据的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'));
html laravel
2个回答
0
投票

您已选择了第一个empsheduling表记录,请查看查询的结尾:first()

要么用first()替换get(),要么在你的视图中删除循环并用$tracking->data获取数据。


0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.