flugscoding
flugscoding

Reputation: 23

LARAVEL - How to redirect if there's an error

I'm making a web application and in the user profile section, I've got a route setup so you go to profile/{id}. When I enter in an IP that doesn't exist on the database, I get this error page: Trying to get property 'avatar' of non-object (View:.

I understand why I get the error, but when I get that error instead of showing the page I want it to redirect back to my dashboard page. How do I do this?

Thanks in advance!

Upvotes: 0

Views: 207

Answers (1)

P3Ri
P3Ri

Reputation: 102

You can test if in your Controller that handle the show function when you get the object from query, check if is null and in true case:

return redirect('/')->with('error', 'Error string');

for example:

  $user = User::find($id);
  if($user === null){
    return redirect('/')->with('error', 'Error string');
  } 

Upvotes: 1

Related Questions