0

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

9
  • Please show your view where you're generating the URL Commented Mar 4, 2024 at 20:05
  • am not generating the url in the view !! Commented Mar 4, 2024 at 20:23
  • Then how are you creating the URL? Where are you getting http://127.0.0.1:8000/collections/1? All you've shown so far are the routes files. Commented Mar 4, 2024 at 20:24
  • Have you tried Route::get('/collections/{nom}', [SousCategorieController::class, 'show']);? Commented Mar 4, 2024 at 20:25
  • I have edited my question I am new to laravel if you can give me a general response maybe I'll get it ? Commented Mar 4, 2024 at 20:29

1 Answer 1

1

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 {
...
}
Sign up to request clarification or add additional context in comments.

1 Comment

Just as a side note, there are many potential problems with this code that I can see just from the limited code in the question. Unnamed routes, using query build joins instead of relationships, non-standard names for models, and what looks like some very inefficient and repetitive database queries.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.