How to validation slug in laravel 5.4.24

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Mastering Slug Validation in Laravel: Ensuring Unique and Clean URLs

As developers building web applications with Laravel, managing URLs correctly is paramount. A well-structured URL, or "slug," not only improves user experience but also ensures proper database integrity. Creating unique slugs requires a combination of smart string manipulation and robust validation logic. This post will guide you through the process of creating, validating, and ensuring the uniqueness of slugs in your Laravel application.

The Anatomy of a Perfect Slug

A slug is a friendly, URL-safe string derived from a title (e.g., My Awesome Post becomes my-awesome-post). For these to be useful in a database context, they must adhere to specific rules: they should be unique across all records and free of characters that cause issues in URLs (like spaces or special symbols).

The process generally involves two main stages: Generation and Validation.

Step 1: Generating the Clean Slug

Before we can validate uniqueness, we must ensure the slug itself is clean. You cannot rely on raw user input directly for database storage; it needs to be processed first.

You mentioned using a function like str_slug(). This is an excellent approach. In Laravel development, while you can write custom helper functions, leveraging established practices ensures consistency across your application. For sophisticated string handling and data manipulation within Laravel, understanding the underlying principles of Eloquent and data handling, as discussed on resources like laravelcompany.com, is key to building scalable features.

Here is how you typically clean up user input for slug generation:

// Example within a Controller or Service class
$userInput = $request->input('title');

// Use Laravel's built-in string functions or custom helpers
$cleanSlug = Str::slug($userInput); // Using the native Laravel helper is often preferred.

// If you need custom hyphenation logic (as suggested in your example):
$db_field_name = str_replace(' ', '-', $cleanSlug); 

Notice how we transform spaces into hyphens and ensure the result is lowercase, which are standard URL conventions.

Step 2: Enforcing Uniqueness via Validation

Once you have generated a clean slug, the critical next step is ensuring that this slug does not already exist in your database table. This is where Laravel's powerful validation system shines. You use the unique rule to enforce this constraint at the application level before hitting the database.

Your proposed validation code snippet is very close to the correct implementation:

$this->validate($request, [
    'company_name' => 'required|unique:admin_users,company_name,slug|max:191',
]);

Let’s break down this rule:

  1. required: Ensures the field is not empty.
  2. unique:admin_users,company_name,slug: This is the core of the uniqueness check. It instructs Laravel to query the admin_users table and verify that no other record exists where the company_name column (or whichever field you are checking against) and the slug column match the submitted value.
  3. max:191: This is a good additional constraint, ensuring the resulting slug stays within reasonable URL length limits.

By placing this rule in your request validation, you prevent duplicate entries from ever reaching your Eloquent model, saving you unnecessary database operations and preventing data corruption.

Best Practices Summary

For robust slug management, follow these best practices:

  1. Generate First: Always clean the input string (lowercase, replace spaces with hyphens) before validation checks to ensure consistency.
  2. Validate Second: Use the unique rule in your form request validation to enforce database uniqueness.
  3. Model Level Checks: For maximum security, consider adding a unique index directly to your database migration for the slug column. This provides a final layer of defense, ensuring data integrity even if application logic is bypassed.

By combining clean string manipulation with Laravel's powerful validation features, you can build reliable and highly maintainable systems. Happy coding!