如何解决尝试使用findOrFail代替get
我想使用get()
而不是findOrFail
,但是它不能那样工作。
那么如果该行不存在,返回404响应的最佳方法是什么?
$log = DB::table('dmlog')
->select(
'dmlog.*','membership.membership','department.department','category.category','communication.communication','room.room AS room','room.category AS room_cat','roommove.room AS roommove','roommove.category AS roommove_cat'
)
->join('membership','membership.id','=','id_membership')
->join('department','department.id','id_department')
->join('category','category.id','id_category')
->join('communication','communication.id','id_communication')
->join('room','room.id','id_room')
->join('room AS roommove','roommove.id','id_roommove')
->where('dmlog.id',$id)->get(); // <----- HERE
return response()->json($log);
解决方法
如果abort_if($log->isEmpty(),404);
为空,则可以在返回行之前添加行$log
终止
findOrFail是一种雄辩的方法,而不是DB Raw类方法。您需要雄辩。
您需要直接引用模型才能使用findOrFail
$model = App\YourModel::findOrFail($id);
https://laravel.com/docs/master/eloquent#retrieving-single-models