Route binding. http://laravel.com/docs/routing#route-model-binding
Routes.php
Route::get('/view/{post_slug}', ['uses' => 'PostController@view']);
Route::bind('post_slug', function($value, $route)
{
// Return a model based on the value in the route URI
return Post::where('slug', $value)->firstOrFail();
});
PostController.php
function view(Post $post)
{
// $post is an instance of Post that is returned from the Route::bind() method.
return View::make('posts.view', ['post' => $post]);
}
I get this error
No query results for model [User]
// Routes
Route::get('post/{slug}, 'PostsController@show');
// PostsController
public function show($slug)
{
$post = Post::whereSlug($slug)->first();
return View::make('posts.show', ['post' => $post]);
}
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community