how to display a name instead of an Id in a URL?

57 Views Asked by At

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

1

There are 1 best solutions below

1
miken32 On

From the documentation (emphasis mine):

Laravel automatically resolves Eloquent models defined in routes or controller actions whose type-hinted variable names match a route segment name. For example:

use App\Models\User;
 
Route::get('/users/{user}', function (User $user) {
    return $user->email;
});

So assuming you are referring to a Sous_categorie model in the URL, you need to define your route like this:

Route::get('/collections/{collection:nom}', [SousCategorieController::class, 'show']);

And your controller method like this:

public function show(Sous_categorie $collection): View {
...
}