I have a route that's look like this /collections/1 and I want it to look like this /collections/name that instance name is stored inside my database and it is unique, I want it to showen in the URL instead of the Id
Here is my Route:
Route::get('/collections/{id}', [SousCategorieController::class, 'show']);
this is my controller code :
public function show(Sous_categorie $sous_categorie, $id)
{
//dd($id);
$count = DB::table('produits')->select('produits.sous_categorie_id')->where('produits.sous_categorie_id', '=', $id)->get();
//return response()->json($count);
$s_marque = DB::table('produits')
->select('marque')
->join('sous_categories', 'sous_categories.id', '=', 'produits.sous_categorie_id')
->whereIn('sous_categories.categorie_id', [$id])->get();
//dd($s_marque);
$f_categorie = DB::table('sous_categories')->select('*')->where('categorie_id', '=', $id)->get();
$s_produits = DB::table('produits')
->select('produits.*', 'sous_categories.categorie_id as laravel_through_key')
->join('sous_categories', 'sous_categories.id', '=', 'produits.sous_categorie_id')
->whereIn('sous_categories.categorie_id', [$id])
->paginate(8)->withQueryString();
//dd($s_produits);
$product = Produit::orderBy('id', 'DESC')->paginate(12)->withQueryString();
$prods = Produit::with('souscategories')->orderBy('id', 'DESC')->paginate(8)->withQueryString();
$categories = Categorie::all();
$sous_categorie = DB::table('sous_categories')->select('*')->where('categorie_id', 'like', '%' . $id . '%')->get();
//$s_produits = DB::table('produits')->select('*')->where('sous_categorie_id','like','%'.$id.'%')->get();
//return response()->json($s_produits);
return view('collections.components', compact('sous_categorie', 'id', 'f_categorie', 'categories', 'prods', 'product', 's_produits', 's_marque', 'count'));
}
Well I tried a various solution like overriding the name using getRouteKeyName like this:
public function getRouteKeyName()
{
return 'nom';
}
}
and changing the route like this:
Route::get('/collections/{collection}', [SousCategorieController::class, 'show']);
But! Am still getting the same route with id http://127.0.0.1:8000/collections/1 instead.
Please help me!
thanks
http://127.0.0.1:8000/collections/1? All you've shown so far are the routes files.Route::get('/collections/{nom}', [SousCategorieController::class, 'show']);?