Support the ongoing development of →
posted 10 years ago
Last updated 2 years ago.

you would do a string replace

URL::action('ArticleController@getView', [$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('ArticleController@getView', [$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 2 years 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 2 years ago.

Perfect thnx Kguner and and tariquesani!

Last updated 2 years ago.

Sign in to participate in this thread!


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.

© 2024 - All rights reserved.