So what is your question?
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'));
}
@Borisu how can i write the route and controller file
example
/lenevo/7 where lenovo is the product name 7 is the id
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
@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'));
}
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.
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
Change the blade files like this
<a href="{{ action('ProductController@Products', ["id" => $products->id, "product" => $products->name]) }}" style="text-decoration:none">
</a>
@Snapey I did not understand sir can you be more clearer. what is that slug column??
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'));
}
@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'));
}
Please or to participate in this conversation.