nazmun
1
I want to delete a user with Laravel Eloquent and return a success or failure message. how do I do that?
/**
* Delete Role Page
*/
public function delete( Request $request ) {
$this->validate($request, [
'user_id' => 'required|exists:users,id'
]);
// Delete the User.
}
You can use the delete method.
User::where('id', '=', $user_id)->first()->delete();
It will return true on delete and false if failed.
if( User::where('id', '=', $user_id)->first()->delete() ) {
return response()->json([
'status' => 1,
'msg' => 'success'
]);
}
else{
return response()->json([
'status' => 0,
'msg' => 'fail'
]);
}
You can do the same with the destroy function.
User::destroy($user_id);