Support the ongoing development of Laravel.io →
Database Eloquent Views
Last updated 2 years ago.
0

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

Last updated 8 years ago.
0

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.

Last updated 8 years ago.
0

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

0

Sign in to participate in this thread!

Eventy

Your banner here too?

eiyu eiyu Joined 26 Jan 2016

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.