Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Shawdow's avatar

route file showing both product name and product id

on the view file

<a href="/products/{{$product->id}}" style="text-decoration:none">

on the route file

Route::get('/products/{product}', 'ProductsController@show');

controller file

public function show(Product $product)
{
    // Laravel will fetch the correct product and return it in the variable $product
    return view('product-page-here', compact('product'));
}
0 likes
11 replies
Borisu's avatar

So what is your question?

2 likes
Shawdow's avatar

@Borisu how can i write the route and controller file

example

/lenevo/7 where lenovo is the product name 7 is the id

biishmar's avatar

route file

Route::get('/products/{product}/{productid}', 'ProductsController@show');

or

Route::get('/products/{product}', 'ProductsController@show');

view file

<a href="/products/{{$product->name}}/{{$product->id}}" style="text-decoration:none">

As in first one you can seperate the search, in second one you cant use route model binding

Shawdow's avatar

@biishmar it shows me error

Parse error: syntax error, unexpected '{'

in the route file

Route::get('/products/{id}/{product}','WelcomeController@Products');

blade file

<a href="{{ action('ProductController@Products',{{$products->name}}/{{$products->id}}) }}" style="text-decoration:none">

</a>

in the controller


public function showProducts($id){
        
         $data = \App\Product::find($id);

         return \view ('Products',compact('data'));
        
    }

Borisu's avatar

Hey @Shawdow, you don't need to put the action() in your href. Just make it

<a href="/products/{{ $product->name }}/{{ $product->id }}">...</a>

Notice the double quote, it's important.

Then since you only need the id to catch a product you can use a wildcard for the route

// in routes/web.php
Route::get('/products/*/{product}', 'ProductsController@show')

// in app/Http/Controllers/ProductsController.php
public function show(Product $product)
{
    return view('products.show', compact('product'))
}

NOTE: This will work only if the product name is on the same table as the id. Otherwise you'll have to do it another way.

Snapey's avatar

The product name also has to be safe for URLs, so you would probably be better off creating a slug column.

For example, you could not link a product name like 15" Laptops

biishmar's avatar

Change the blade files like this

<a href="{{ action('ProductController@Products', ["id" => $products->id, "product" => $products->name]) }}" style="text-decoration:none">

</a>
Shawdow's avatar

@Snapey I did not understand sir can you be more clearer. what is that slug column??

Shawdow's avatar

@biishmar

i have tried with your solution its working but not as expected one

In the url the product/lenevo/7 it displays the lenevo products for the id 7 that's fine another product suppose samsung with id 8 is present if i change the url as /lenovo/8 (8 is the samsung products id) it displays the samsung product it should not display until the url is /samsung/8

any idea for the problem??

route file

Route::get('/products/{id}/{product}','ProductController@Products');

blade file

<a href="{{ action('ProductController@Products', ["id" => $products->id, "product" => $products->name]) }}" style="text-decoration:none">

in the controller file

public function Products($id){
        
         $data = \App\Product::find($id);
         
         return \view ('showProducts',compact('data'));
        
    }

biishmar's avatar

@Shadow because you r using id to find product in controller thats why even if u change the name the product comes as id...

route and blade is fine change the controller like this

public function Products($id, $name){
        
         $data = \App\Product::where('id', $id)->where('name', $name)->first();
         
         return \view ('showProducts',compact('data'));
        
    }

this code get the product by id and name..

Slug is used behalf of id

for example if there is product name apple iphone X, u trying to display name and id, so u can use id to get product like

https://www.somesite.com/product/apple-iphone-X/7

if u create a column slug in product table and save the product name like apple-iphone-X u dont have to use id just use

https://www.somesite.com/product/apple-iphone-X

Route

Route::get('/products/{slug}/','ProductController@Products');

blade

<a href="{{ action('ProductController@Products', ["slug" => $products->slug]) }}" style="text-decoration:none">

In controller

public function Products($slug){
        
         $data = \App\Product::where('slug', $slug)->first();
         
         return \view ('showProducts',compact('data'));
        
    }
Shawdow's avatar

@biishmar awesome explanation sir i think its better to use slug according to @snapey sir replied so i have tried it

it shows me error Undefined property: stdClass::$slug

Please or to participate in this conversation.