从两个函数获取结果并将其显示在一个视图上

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

我有一个数据库用户记录,其中包含列:user_id 和supervisor_id。对于登录用户来说,他/她可以是用户,也可以是其他人的监督者。 我想显示两个表,其中表 1 将显示具有登录用户的 user_id 的所有用户记录,表 2 显示具有登录用户的supervisor_id 的所有用户记录。

这是我在控制器上的代码。

public function user_record()
    {
        $user_id = Auth::user()->id;

        $show_leave= DB::table('user_record')->where('user_id','=',$user_id)->get();

    }
    public function show_user_record(Request $request)
    {
        $user_id = Auth::user()->id;

        $show_user_record= DB::table('user_record')->where('supervisor','=',$user_id )->get();

        $user_record=$this->user_record();

        return view('/user_records', compact('show_user_record','user_record'));
    }

user_records 刀片文件:

<table>
<tr>
<th>#</th>
<th>Full Name</th>
<th>Status</th>
<th>Remarks</th>
</tr>
@foreach($show_user_record as $data)
<tr>
<td>{{$data->id}}</td>
<td>{{$data->full_name}}</td>
<td>{{$data->status}}</td>
<td>{{$data->remarks}}</td>
</tr>
@endforeach
</table>

<table>
<tr>
<th>#</th>
<th>Full Name</th>
<th>Status</th>
<th>Remarks</th>
</tr>
@foreach($user_record as $user_data)
<tr>
<td>{{$user_data->id}}</td>
<td>{{$user_data->full_name}}</td>
<td>{{$user_data->status}}</td>
<td>{{$user_data->remarks}}</td>
</tr>
@endforeach
</table>

错误: foreach() 参数必须是 array|object 类型,给定 null @foreach($user_record 为 $user_data)

laravel eloquent laravel-blade
1个回答
0
投票

您没有在 user_record() 函数中返回任何内容,因此它是 null ,并且 foreach() 接受某种集合。

第一个修复是在方法中使用 return


public function user_record()
    {
        $user_id = Auth::user()->id;

        $show_leave= DB::table('user_record')->where('user_id','=',$user_id)->get();
        return $show_leave;
    }
© www.soinside.com 2019 - 2024. All rights reserved.