Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

keroherox30's avatar

laravel: Showing the slug in URL instead of ID - List of Products

Hello,

i want to remove id and want only slug how will be?! i trying but get 404 error

Model Products

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Product extends Model
{
    use SoftDeletes;

    protected $guarded = ['id'];

    protected $casts = [
        'extra_descriptions' => 'array',
        'specification' => 'array',
    ];

 public function categories()
    {
        return $this->belongsToMany(Category::class, 'products_categories', 'product_id', 'category_id');
    }

    public function orderDetails()
    {
        return $this->hasMany(OrderDetails::class);
    }


    public function orders()
    {
        return $this->hasMany(Order::class);
    }

}

Product Controller


    public function productDetails($id, $order_id =null)
    {
        $product = Product::where('id', $id)->where('status', 1)
                    ->with('categories'')
                    ->whereHas('categories')
                    ->firstOrFail();

        $review_available = false;

        if($order_id){
            $order = Order::where('order_number', $order_id)->where('user_id', auth()->id())->first();
            if($order){
                $od = OrderDetail::where('order_id', $order->id)->where('product_id', $id)->first();
                if($od){
                    $review_available = true;
                }
            }
        }
        $pageTitle = 'Product Details';
        return view($this->activeTemplate . 'product_details', compact('product', 'pageTitle', 'review_available ');
    }

Route

Route::get('product/detail/{id}/{slug}/', 'ShopController@productDetails')->name('product.detail');

i want help and thanks

0 likes
9 replies
tykus's avatar

@keroherox30 are you going to delete this thread whenever you get the answer - like you did the last one?

keroherox30's avatar

@tykus

this not me this is my young brother because he try upload image but he not know the new account created

i hope accept my apology

Snapey's avatar

your route accepts id and slug

'product/detail/{id}/{slug}/'

but this is not matched in your controller

public function productDetails($id, $order_id =null)
keroherox30's avatar

@Snapey

what if i want to encrypt id without change all things and wrong something

like product/encrypt letters here /name-product

i do already encrypt but i got more letters i want to low letters

how to do this ?

MohamedTammam's avatar

Route

Route::get('product/detail/{slug}', 'ShopController@productDetails')->name('product.detail');

Controller

public function productDetails($slug)
{
        $product = Product::where('slug', $slug)
		// ...

Please or to participate in this conversation.