I want to change the link urls in my laravel project from:
www.mywebsite.com/products/1/1
to
www.mywebsite.com/products/NameOfcategoryOfProduct/NameOfProduct
Here is my routes/web.php content:
Route::get("products", 'ProductController@index')->name('products.index');
Route::get("products/{groupement}/{group}", 'ProductController@category')->name('products.categories');
Route::get("products/{groupement}/{group}/{category}", 'ProductController@product')->name('products.list');
Route::get("products/{groupement}/{group}/{category}/{product}", 'ProductController@detail')->name('product.detail');
Here is my products index page:
@section('products-list')
<div >
@foreach ($groupements as $groupement)
<div >
<div style="border-color: #{{$groupement->color_groupement}}">
<div >
<div>
<h3 style="color: #{{$groupement->color_groupement}}">{{$groupement->name_groupement}}</h3>
<span>{{$groupement->description}}</span>
</div>
<div style="width: 250px">
<img src="{{asset('img/group/'.$groupement->img_groupement.'')}}" alt="{{$groupement->name_groupement}}" style="width: 250px">
</div>
</div>
<div >
<div >
@foreach ($groups as $group)
@if ($group->id_groupement == $groupement->id)
<div >
<div >
<a href="{{route('products.categories', ["groupement"=>$groupement->id, 'group' => $group->id])}}">{{$group->name_group}}</a>
</div>
</div>
@endif
@endforeach
</div>
</div>
</div>
</div>
@endforeach
</div>
@endsection
Can you help me with that? let me know if I need to add more explanations or source code to other ressources.
CodePudding user response:
What you need to do is use the name of the columns that you want to show there. Let's say you have $product->category
and $product->name
. That means your route will be:
Route::get("products/{category}/{name}", 'ProductController@detail')->name('product.detail');
And your controller will look like this:
public function detail($category, $name){
And of course, when you call the route, you must use the category and the name instead of the id
now.
CodePudding user response:
In route bindings, pass NameOfcategoryOfProduct and NameOfProduct instead of IDs.
In the controller instead of doing a Find and looking for the IDS use a where and look for NameOfcategoryOfProduct and NameOfProduct
Like this
<a href="{{route('products.categories', ["groupement"=>$groupement->NameOfProduct, 'group' => $group->name_group])}}">{{$group->name_group}}</a>
Dont forget make changes in your controller