Looks fine from here.
Do you have a resource controller overwriting the show method, by any chance?
Do you have something like this in your routes.php file?
Route::resource('products', 'ProductsController');
Try checking your routes using php artisan route:list
Thanks @Neoglyph, I dont have resource controller I dont know how to use it I'm using laravel 5.2, do you know where to learn it?. Currently i'm using two parameter like this
Route::get('/categories/{id}/{nama}', function($id, $nama) {
$category = Category::find($id);
return $this->content = view('/listCategory', array(
'products' => $category->paginate(2),
'categories' =>Category::all()
));
});
it works but the id and the category name both exposed in the uri.
To get back to the first issue...
Route::get('/products/{productname}', array( 'as' => 'whatever', 'uses' => 'ProductsController@show' ));
public function show($productname) {
$product = Product::findOrFail($productname);
return $this->content = view('products.product', array( 'product' => $product ));
}
If you were trying to search by a string parameter - i.e. productname, the findOrFail would not work, since it only excepts an integer (as in id).
Since you're using 5.2, if you wanted to search by either an id or the name (I would recommend using a slug instead of a name), you would have to define a route binding in the RoutesProvider.php file.
RoutesProvider.php
/**
* Define your route model bindings, pattern filters, etc.
*
* @param \Illuminate\Routing\Router $router
* @return void
*/
public function boot(Router $router)
{
//
parent::boot($router);
\Route::bind('products', function($parameter) {
if (is_numeric($parameter)) {
return Product::findOrFail($parameter);
}
// I reccomend using slug instead of an actual product name with whitespaces and uppercase characters
return Product::where('slug','=',$parameter)->firstOrFail();
});
}
I would then change your route file.
Route::get('/products/{products}', array( 'as' => 'whatever', 'uses' => 'ProductsController@show' ));
public function show($products) {
return $this->content = view('products.product', array( 'product' => $products ));
}
If you need more info on route bindings and how it works, you can find it here
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community