Mastering Str::slug() in Laravel

Harris Raftopoulos
2 min readJul 2, 2024

Laravel developers, here’s a gem for you: 💎
Today, we’re diving into Str::slug(), a powerful helper function in Laravel that effortlessly converts any string into a URL-friendly slug. This function is perfect for improving SEO and creating clean, readable URLs. Let’s explore how to use Str::slug() effectively in your Laravel projects.

Understanding Str::slug()

The Str::slug() function is part of the Illuminate\Support\Str class. It converts a given string into a URL-friendly format by replacing spaces and special characters with hyphens and removing any non-ASCII characters. This ensures your URLs are clean, readable, and optimized for search engines.

Basic Usage

Let’s start with a basic example to see Str::slug() in action:

Illuminate\Support\Str;

$title = "Learn Laravel in 10 Days!";
$slug = Str::slug($title);
echo $slug; // Output: learn-laravel-in-10-days

In this example, Str::slug() takes a string with spaces and special characters and converts it into a lowercase, hyphen-separated string. This is ideal for creating SEO-friendly URLs for blog posts, products, or any other content.

Real-Life Example

Imagine you are building a blogging platform and want to generate slugs for your blog post titles automatically. Here’s how you can achieve this using Str::slug():

use Illuminate\Support\Str;

class BlogPostController extends Controller
{
public function store(Request $request)
{
$title = $request->input('title');
$slug = Str::slug($title);

// Save the post with the generated slug
$post = new BlogPost();
$post->title = $title;
$post->slug = $slug;
$post->content = $request->input('content');
$post->save();

return redirect()->route('posts.show', $slug);
}
}

In this scenario, when a new blog post is created, the store method generates a slug from the title using Str::slug() and saves it to the database. This slug is then used in the URL to view the post, ensuring a clean and SEO-friendly URL structure.

Customizing the Separator

By default, Str::slug() uses a hyphen as the separator. However, you can customize this by passing a second argument to the function:

use Illuminate\Support\Str;

$title = "Learn Laravel in 10 Days!";
$slug = Str::slug($title, '_');

echo $slug; // Output: learn_laravel_in_10_days

In this example, the separator is changed to an underscore, resulting in a different format for the slug.

Conclusion

The Str::slug() function in Laravel is an essential tool for generating URL-friendly slugs from strings. Whether you’re optimizing URLs for SEO, creating clean and readable links, or standardizing input data, Str::slug() provides a simple and effective solution. Give it a try in your next project and see how it can enhance your application’s usability and SEO performance.

--

--

Harris Raftopoulos

Senior Software Engineer | 15+ years in PHP, 11+ with Laravel | Expert in Livewire, TailwindCSS & VueJS | DevOps Enthusiast | 🎤Speaker at Athens Laravel Meetup