user3384514
user3384514

Reputation: 297

passing parameter in laravel

i have an index pages with a link_to:

<h1> User:</h1>
        @foreach ($user as $user)

            {{link_to("/users/{$user->username}",$user->username)}}
        @endforeach

then i have a route:

Route::get('/users/{{$username}}', function($username){

        return View::make('show')->with('username',$username);

});

Now, if i understand clear, i am passing username as parameter to function, and username is my url, now if i pass parameter to my show view,

<body>
    <div>       

        <h1> User:</h1>
            {{$username}}
    </div>
</body>
</html>

I should be able to see it in my page. Where i am wrong? I can't take a parameter from the url when i use get? Why i need to do:

Route::get('/users/{{$username}}', function($username){
$user=User::whereUsername($username)->fist();

        return View::make('show')->with('username',$user);
});

Upvotes: 0

Views: 113

Answers (1)

Antonio Carlos Ribeiro
Antonio Carlos Ribeiro

Reputation: 87719

Your route is wrong, this is the correct one:

Route::get('/users/{username}', function($username){

    $user=User::whereUsername($username)->fist();

    return View::make('show')->with('user',$user);

});

It's just

/users/{username}

and not

/users/{{$username}}

Also, you view will receive an user object, so you have to:

 <div>       
     <h1> User:</h1>
         {{$user->username}}
 </div>

EDIT

In Laravel there are 2 kind {}:

1) In views you have to use {{}} or {{{}}} (escaped version). Inside them you put PHP code:

{{$variable}}

{{ isset($variable) ? $variable : 'default value' }}

2) In routes, you just use {} and inside it it's not PHP, just a route parameter name, without $:

/user/{name}

/user/{id?} (in this case id is optional, might or might not be send)

Upvotes: 2

Related Questions