这是我的SQL查询
select * from users us join EmailBox em on us.userid = em.suserid
where em.status = "Delete" and (em.suserid="$userid" or em.riemailid="$user")
如果" suserid"我想检查任何一个字段。或" riemailid"只有结果。 所以我在laravel querybuilder中改变了这个。
$lists = EmailBox::join('users as us', 'EmailBox.suserid', '=', 'us.id')
->where('status' , 'Delete')
->where(function($query){
$query->where('suserid' , '$userid');
$query->orwhere('riemailid' , '$user');
})->paginate(5);
当直接传递值时,此查询正常工作, userid直接传递1个手段
$lists = EmailBox::join('users as us', 'EmailBox.suserid', '=', 'us.id')
->where('status' , 'Delete')
->where(function($query){
$query->where('suserid' , 1);
$query->orwhere('riemailid' , '$user');
})->paginate(5);
这是我在laravel中的代码。 什么是错误?
if(Auth::check()){
//if(Auth::user()->email!="")
$user=Auth::user()->email;
$userid=Auth::user()->id;
$lists = EmailBox::join('users as us', 'EmailBox.suserid', '=', 'us.id')
->where('status' , 'Delete')
->where(function($query){
$query->where('suserid' , '$userid');
$query->orwhere('riemailid' , '$user');
})->paginate(5);
$links = $lists->render();
return view('front.index', compact('lists', 'links'));
}
我收到错误$ userid变量未定义。
答案 0 :(得分:4)
->where(function($query) use($userid, $user){
$query->where('suserid' , $userid);
$query->orwhere('riemailid' , $user);
}
答案 1 :(得分:0)
您需要在查询中使用“use”关键字。
还要写下如下查询: -
EmailBox::join('users as us', 'EmailBox.suserid', '=', 'us.id')
->where('status' , 'Delete')
->where(function($query) use ($userid, $user){
$query->where('suserid' , $userid)
->orwhere('riemailid' , $user);
})->paginate(5);
希望它会对你有所帮助:)。
答案 2 :(得分:0)
确保变量不写入'' (单引号)
if(Auth::check()){
//if(Auth::user()->email!="")
$user=Auth::user()->email;
$userid=Auth::user()->id;
$lists = EmailBox::join('users as us', 'EmailBox.suserid', '=', 'us.id')
->where('status' , 'Delete')
->where(function($query){
$query->where('suserid' , $userid);
$query->orwhere('riemailid' , $user);
})->paginate(5);
$links = $lists->render();
return view('front.index', compact('lists', 'links'));
}