Php 对数组laravel上的成员函数where()的调用
我有两张桌子tbl\U法律硕士和tbl\U法律硕士 在这两个表上都有一个名为的列分配给了,该列存储用户id 我的任务是通过加入tbl_law_master的law_id从tbl_law_sub_master获取特定用户id 这是我的密码Php 对数组laravel上的成员函数where()的调用,php,mysql,laravel,join,controller,Php,Mysql,Laravel,Join,Controller,我有两张桌子tbl\U法律硕士和tbl\U法律硕士 在这两个表上都有一个名为的列分配给了,该列存储用户id 我的任务是通过加入tbl_law_master的law_id从tbl_law_sub_master获取特定用户id 这是我的密码 $law_id = $_GET['law_id']; $sublaw = DB::table('tbl_law_sub_master') ->select('tbl_law_sub_master.*', 'tbl_law_mast
$law_id = $_GET['law_id'];
$sublaw = DB::table('tbl_law_sub_master')
->select('tbl_law_sub_master.*', 'tbl_law_master.lm_id', 'tbl_law_master.law_name', 'tbl_law_master.id as lawId')
->leftJoin('tbl_law_master', 'tbl_law_master.id', '=', 'tbl_law_sub_master.lm_id')
->where('tbl_law_sub_master.lm_id', $law_id)
->orderBy('type_of_event', 'asc')
->orderBy('section', 'asc')
->orderBy('rules', 'asc')
->orderBy('notification', 'asc')
->orderBy('circular', 'asc')->get();
if (!in_array(Auth::user()->role, [1, 7]))
{
$sublaw = $sublaw->where('tbl_law_master.assigned_to', Auth::user()->id);
}
它向我显示错误为
调用数组上的成员函数where()
我想你应该在最后一个
where
语句之后调用get
$sublaw = DB::table('tbl_law_sub_master')
->select('tbl_law_sub_master.*', 'tbl_law_master.lm_id', 'tbl_law_master.law_name', 'tbl_law_master.id as lawId')
->leftJoin('tbl_law_master', 'tbl_law_master.id', '=', 'tbl_law_sub_master.lm_id')
->where('tbl_law_sub_master.lm_id', $law_id)
->orderBy('type_of_event', 'asc')
->orderBy('section', 'asc')
->orderBy('rules', 'asc')
->orderBy('notification', 'asc')
->orderBy('circular', 'asc');
if (!in_array(Auth::user()->role, [1, 7]))
{
$sublaw = $sublaw->where('tbl_law_master.assigned_to', Auth::user()->id)->get();
}
问题是您已经调用了查询的
get()
,然后再次调用了where()
您应该像这样使用where子句函数
$sublaw = DB::table('tbl_law_sub_master')
->select('tbl_law_sub_master.*', 'tbl_law_master.lm_id', 'tbl_law_master.law_name', 'tbl_law_master.id as lawId')
->leftJoin('tbl_law_master', 'tbl_law_master.id', '=', 'tbl_law_sub_master.lm_id')
->where(function($query) use ($law_id) {
$query->where('tbl_law_sub_master.lm_id', $law_id);
if (!in_array(Auth::user()->role, [1, 7]))
{
$query->where('tbl_law_master.assigned_to', Auth::user()->id);
}
})
->orderBy('type_of_event', 'asc')
->orderBy('section', 'asc')
->orderBy('rules', 'asc')
->orderBy('notification', 'asc')
->orderBy('circular', 'asc')->get();
那么问题出在哪里?@MahdiYounesi的问题是我的代码没有返回特定用户id的数据