0

i am new to laravel and i am having trouble getting the user info by going to the route 'user/{username}';

theoretically it should work, this is my route:

Route::get('/user/{username}', function($username){
    $user = User::where('username',$username) -> first();
    if(isset($user['username'])){

        return redirect()->route('/user/'.$username, ['user' => $user]);

    }else{

        return redirect()->route('/user/'.$username, ['error' => 'This User Does Not Exist']);

    }
});

i did use App\User; in the page so it should work. i am getting this error:
NotFoundHttpException in RouteCollection.php line 161:

Any help is appreciated, thank you, please let me know if u need any more information in order to help.

red security
  • 193
  • 3
  • 14

3 Answers3

2

Redirect takes a routes name, not the path you wish to redirect to. In your code, add a name for your route, like this:

Route::get('/user/{username}', function($username) {
    [...]
})->name('username');
// Here we call our route 'username'

Now you can do redirect like this:

// Note that we call redirect with the name we just gave it, passing the username value forward
return redirect()->route('username', ['username' => $user]);

Edit: Looking at your code I do not fully understand what you wish to do. The code looks like it would create a redirect loop? It looks like if you find a user, you redirect back to the same route?

OptimusCrime
  • 14,662
  • 13
  • 58
  • 96
  • concerning your question that is not what i meant to do, that was my main error i am trying to get the user info when u go to user/username and display them on the screen, if there is no info then don't display then tell the user that there is no user with that username basically – red security Feb 13 '17 at 20:55
  • Thanks @redsecurity . I am not too familiar with Laravel, but as I've understood it, you do not need to redirect to do this. The redirect will send the user forward as if (s)he did a new request. You just wish to display different information depending on if a user with the given username was found? If so, redirects are not the correct thing to use here. You can just render your view and have a simple else/if in your template. – OptimusCrime Feb 13 '17 at 20:59
  • that is correct as well, i should use return view('viewname') instead of return redirect()->route() – red security Feb 13 '17 at 21:01
  • @redsecurity Yes. You should do `view('your-template', ['name' => $user]);` or something like that. The tutorials are pretty good at explaining this. – OptimusCrime Feb 13 '17 at 21:12
  • i accepted and upvoted your answer thanks for the help – red security Feb 13 '17 at 21:19
0

You should use firstOrFail. This method will throw a 404 error if no user is found in the database.

Call to user column value with ->. So your code should look like this :

Route::get('/user/{username}', function($username){
    $user = User::where('username',$username)->firstOrFail();
    return view('viewname', ['user' => $user]);
}

Don't forget to create a view to your user page. You should also create a controller to handle thsi request.

Adraqi
  • 51
  • 6
  • i tried to put this request in my UserController but i have no idea how to implement it, i wanted to give it 'middleware' => 'auth' as well – red security Feb 13 '17 at 20:53
  • This worked but how can i handle the error if i get one ? i get this error if there is no user with that username NotFoundHttpException in Handler.php line 131: No query results for model [App\User]. – red security Feb 13 '17 at 20:57
  • It's because you're in dev environement. There will be just a 404 error in production. You can use a middleware like this : Route::get('/yourroutehere', 'UserController@methodName')->middleware('auth'); – Adraqi Feb 13 '17 at 21:40
0
$user = User::where('username', '=', $user)->first();

try with this..

Rid Islam
  • 142
  • 5