Support the ongoing development of →
posted 9 years ago
Last updated 1 year ago.

you would do a string replace

URL::action('[email protected]', [$article->id, str_replace(' ', '-', $article->title)])

I have a method on my models to create slugs, it handles other cases such as multiple spaces, international characters, lowercasing, and strips punctuation and special characters.

public function slug()
    $clean = $this->title;
    setlocale(LC_ALL, 'en_US.UTF8');

    $delimiter = '-';

    $clean = iconv('UTF-8', 'ASCII//TRANSLIT', $clean);
    $clean = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $clean);
    $clean = preg_replace("/[\/_|+ -]+/", $delimiter, $clean);
    $clean = strtolower(trim($clean, $delimiter));

    return $clean;

I got the method from the following url:

Then you would use it like:

URL::action('Arti[email protected]', [$article->id, $article->slug()])

Also, if your article names are unique, you can save the slug to the database and drop the id from the url, and search for article by slug instead of id. Make sure the slug is also unique.

Last updated 1 year ago.

What you want is a slug. Consider using or if you want to code your own use Str::slug to create slug from Title - the syntax is Str::slug($title,$separator = '-')


Last updated 1 year ago.

Perfect thnx Kguner and and tariquesani!

Last updated 1 year ago.

Sign in to participate in this thread!

Full Stack Europe

Your banner here too?

brownpixel brownpixel Joined 15 Feb 2014


We'd like to thank these amazing companies for supporting us

Your logo here?

The Laravel portal for problem solving, knowledge sharing and community building.

© 2023 - All rights reserved.