php - Laravel 8: Getting 404 “Not Found” Page While Trying To Delete User
one text
Solution:
There are 2 options left what can be wrong, either you can’t find the user. Or you can’t find the photo. Based on the provided information it think there is no photo. The method findOrFail
will return a 404 when the model is not found, see also https://laravel.com/docs/8.x/eloquent#not-found-exceptions.
So what you can do is add a dd
before the photo check to see if the user is found. If that is true you can be sure it is the photo model. If that is the case, you should use Photo::find($user->photo_id)
.
After that you can wrap your unlink in an if statement where you Check if the photo model is not empty
public function destroy($id)
{
$user = User::findOrFail($id);
$photo = Photo::find($user->photo_id);
if (!empty($photo)) {
unlink(public_path() . $user->photo->path);
$photo->delete();
}
$user->delete();
return redirect('admin/users');
}
Source