mercredi 27 juillet 2016

Laravel error handling when route does not exist

I have a route to show my users' profile in my laravel project. But when you go to an url and fill in a username that does not exist it gives a nasty error, obviously because that username doesn't exist in the database.

Has anyone any idea how I can error handle this?

Here's my route:

Route::get('user/{name}', 'userController@showUser');

Here's my function:

public function showUser($name)
    {
        $user = User::where('name' , '=', $name)->firstOrFail();
        return view('user.show', compact('user'));
    }

This is what I've tried but doesn't seem to work since I get this error: View not found

$user = User::where('name' , '=', $name)->first();
        if(!empty($user)){
            return view('user.show', compact('user','projects'));
        }else{
            return view('user');
        }

Aucun commentaire:

Enregistrer un commentaire