Laravel Hide ID from url

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Mastering Clean URLs: Hiding IDs from Your Laravel Routes with Slugs

As developers building modern web applications, one of the most immediate ways to improve user experience and Search Engine Optimization (SEO) is by adopting clean, human-readable URLs. Seeing cryptic numeric IDs like /store/1/details instead of meaningful names like /store/cafe-crsip-chicago feels clunky and unprofessional.

This guide will walk you through the best architectural approach in Laravel to hide internal database IDs from your public URLs by utilizing slugs, ensuring your application adheres to clean URL conventions while maintaining robust data integrity.

The Problem: Numeric IDs vs. Semantic URLs

Your current setup relies on traditional routing where the ID is exposed directly: http://example.com/store/1/Cafe-Crsip-Chicago-60640. While functional, this approach ties your public interface directly to your database structure, which is poor practice for maintainability and SEO.

The goal is to shift from an ID-based routing strategy to a slug-based routing strategy. This involves generating a unique, URL-friendly string (the slug) from your relational data and using that string as the primary identifier in the URL path.

The Laravel Solution: Generating and Using Slugs

To achieve this transformation cleanly within the Laravel ecosystem, we need a process that maps dynamic database attributes to static URL segments. This is typically handled on the Model or within a dedicated service layer before the request hits the controller.

Step 1: Creating the Slug Strategy

Instead of relying on manual string manipulation inside your route definition (which, as you noted, can become brittle), we should leverage Laravel’s Eloquent capabilities and potentially custom accessors to handle slug generation consistently.

For your specific case involving store_name, store_city, and store_zipcode, the process involves concatenating these fields into a single, URL-safe string.

Here is how you might structure the logic within your Model or a Form Request:

// Example concept in a Store Model
public function getSlugAttribute()
{
    // Combine relevant fields and sanitize them for use in a URL slug
    $name = Str::slug($this->store_name);
    $city = Str::slug($this->store_city);
    $zip = Str::slug($this->store_zipcode);

    // Create a unique, URL-friendly slug
    return "store/{$name}-{$city}-{$zip}";
}

Step 2: Updating the Route Definition

Once you have a clean string (the slug) generated by your model, you update your route to use this slug as the primary dynamic parameter. This shifts the focus from querying by an auto-incrementing id to finding a record based on its human-readable identifier.

Instead of routing based on an integer ID:

// Old Route (ID-based)
Route::get('store/{id}/{slug}', ...);

You route directly using the desired slug structure:

// New Route (Slug-based)
Route::get('store/{slug}', ['as' => 'showStoreDetails', 'uses' => 'StoreController@show']);

Step 3: Implementing Controller Logic

In your controller, instead of fetching by an integer ID, you will now fetch the record using the provided slug. Laravel’s Eloquent makes this very straightforward if you define a custom relationship or use Model Binding rules.

use App\Models\Store;

class StoreController extends Controller
{
    public function show(string $slug)
    {
        // Find the store by its generated slug
        $store = Store::where('store_name', 'LIKE', "%{$slug}%") // Or better, use a dedicated slug column if you implemented it fully
                    ->firstOrFail();

        // Now, use the retrieved data directly.
        return view('stores.show', compact('store'));
    }
}

Best Practices for URL Management in Laravel

When dealing with complex relational data and clean URLs, remember that this is more than just routing; it’s about data integrity. As you build larger applications using frameworks like Laravel, focusing on Eloquent relationships and strong validation ensures that your slugs are consistent and unique across the entire application. Always prioritize semantic identifiers (like slugs) over raw database keys for public-facing URLs.

Conclusion

By embracing slug generation as a core part of your data management workflow, you transform your application from being database-centric to being user-experience-centric. This approach results in cleaner, more SEO-friendly URLs, better maintainability, and a significantly improved experience for both your users and search engine crawlers. Start by defining clear rules for slug creation within your models, and let Laravel handle the rest of the routing magic!