我尝试了下面的代码,但是没有用。
在此代码中,当我在浏览器“ URL”中输入“ DELETED”,“ id”数字时,它也显示“ DELETED”状态的用户详细信息,我只想显示和访问“ ACTIVATE”和“ DEACTIVATE”用户我在“ URL”中输入了“ DELETED”用户“ id”,应该显示错误:
public function staff_status($id)
{
$user = User::where('status', '=', 'ACTIVATE')->orWhere('status', '=', 'DEACTIVATE')->find($id);
return view('home', compact('user'));
}
public function status_update(Request $request, $id)
{
$user = User::where('status', '=', 'ACTIVATE') > orWhere('status', '=', 'DEACTIVATE')->find($id);
$user['status'] = $request->status;
$user->save();
return back()->with('success', 'SUBMITED SUCCESSFULLY .');
}
答案 0 :(得分:0)
如@ Chay22所述,您应该在条件中添加另一个条件
只需将您的代码替换为此
public function staff_status($id)
{
$user = User::where('status', '!=', 'DELETED')
->where(function($query){
$query->where('status','=','ACTIVATE');
$query->orWhere('status','=','DEACTIVATE');
})->find($id);
if (! $user) {
return // return your error here
}
return view('home',compact('user'));
}
public function status_update(Request $request, $id)
{
$user = User::where('status', '!=', 'DELETED')
->where(function($query){
$query->where('status','=','ACTIVATE');
$query->orWhere('status','=','DEACTIVATE');
})->find($id);
if ($user) {
$user->status = $request->status;
$user->save();
return back()->with('success','SUBMITED SUCCESSFULLY .');
} else {
return // return your error here
}
}