How can I change the URL in laravel

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Mastering Dynamic URLs in Laravel: From IDs to Slugs

As a developer, one of the most frustrating yet crucial tasks when building web applications is managing clean, SEO-friendly URLs. You have an internal database using simple integer IDs (like /blog/1), but users expect human-readable paths (like /blog/my-awesome-post). This process, known as creating "slugs," is essential for good user experience and search engine optimization.

If you are working with Laravel, achieving this dynamic URL restructuring seems complex at first, especially when starting out. Don't worry; this is a very common requirement, and Laravel provides elegant tools to handle it. As we explore how to achieve this transformation, we will walk through the necessary steps using modern Laravel principles.

Understanding the Concept: IDs vs. Slugs

The core issue you are facing is the difference between an Identifier (the database primary key, e.g., 1) and a Slug (a human-readable string, e.g., my-blog-name). The goal is to use the slug in the URL while still referencing the internal ID for database operations.

We need a mechanism to map the two seamlessly. This mapping happens primarily within your Model and Route definitions.

Step 1: Database Preparation (The Foundation)

Before we write any code, your database structure must support this change. You need a column specifically for the URL-friendly name.

Let's assume you have a blogs table.

CREATE TABLE blogs (
    id INT AUTO_INCREMENT PRIMARY KEY,
    title VARCHAR(255) NOT NULL,
    slug VARCHAR(255) UNIQUE NOT NULL, -- This is our new column for the URL
    content TEXT
);

When you create a blog entry, you must generate and store this slug. In Laravel, this generation logic usually happens when saving the model.

Step 2: Generating the Slug in Your Controller/Model

The most critical part is ensuring that when a record is created or updated, the slug is generated correctly (usually by converting the title to lowercase, replacing spaces with hyphens, and removing special characters).

In your Eloquent Model (app/Models/Blog.php), you can use accessor methods to handle this transformation cleanly:

// app/Models/Blog.php

use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Support\Str;

class Blog extends Model
{
    /**
     * Get the slug attribute.
     */
    protected function slug(): Attribute
    {
        return Attribute::make(
            get: fn ($value) => Str::slug($value)
        );
    }
}

This setup ensures that whenever you retrieve a blog, Laravel automatically generates the correct URL-friendly string from the stored title.

Step 3: Defining Dynamic Routes (The Magic of Laravel)

Now we define the route to use this new slug instead of just the ID. In your routes/web.php file, instead of relying solely on simple parameter matching, you will bind your dynamic routes directly to the model.

For a blog detail view, you would define a route that expects the slug:

// routes/web.php

use App\Http\Controllers\BlogController;

// Route definition using the slug as the parameter
Route::get('/blog/{blog:slug}', [BlogController::class, 'show']);

Notice how we used {blog:slug}. This tells Laravel that the segment in the URL must match a column named slug on the model being resolved.

Step 4: Retrieving and Displaying the Data

In your controller method (show), you retrieve the blog using the provided slug, and then you can use the ID to fetch all related data from the database.

// app/Http/Controllers/BlogController.php

use App\Models\Blog;
use Illuminate\Http\Request;

class BlogController extends Controller
{
    public function show(string $blogSlug)
    {
        // Find the blog using the slug we received from the URL
        $blog = Blog::where('slug', $blogSlug)->firstOrFail();

        // Now you can use $blog->id for database operations, 
        // but the URL remains clean: /blog/my-blog-name
        return view('blogs.show', compact('blog'));
    }
}

Conclusion

Changing dynamic URLs in Laravel is not a single trick; it's an architectural decision that involves harmonizing your database schema, Eloquent models, and routing definitions. By implementing proper slug generation on save, defining routes to accept slugs, and using Route Model Binding, you create a robust, clean, and highly maintainable URL structure.

Remember, building scalable applications is key. As you continue your journey with Laravel, remember that leveraging the framework's built-in conventions—like Eloquent relationships and route definitions—will save you countless hours. For deeper dives into structuring complex application logic, exploring resources from https://laravelcompany.com is highly recommended. Happy coding!