Support the ongoing development of Laravel.io →
Eloquent Packages
Last updated 2 years ago.
0

Thanks for that.

So essentially he just made a new column in the database called slug. Entered the name-of-the-slug in the table and instead of making the query on the ID field, passed the slug instead?

0

ottz0: here's a slug package that integrates nicely with eloquent models: https://github.com/cviebrock/eloquent-sluggable

Say you have a table column named "name" and want to slug it.

First add the new column by any means you'd like, let's call it "name_slug".

Next, in the eloquent model you would add something like this:

    protected $sluggable = array(
        'build_from' => 'name',
        'save_to'    => 'name_slug',
        'on_update'  => true,
    );

the on_update will automatically update the slug field if on a normal $artist->save(); by default it will be automatically populated only on a create() call on the model.

if you want some seo fancy routing, you could use something like this in your routes.php (place it in the bottom of your routes.php or it will interfere with other routes):

Route::get('/{name}', 'ArtistController@showSlug')->where('name', '[A-Za-z-]+');

in your ArtistController, you could redirect the slug view to your existing "view by id" view, like

    public function showSlug($slug)
    {
        $artist = Artist::findBySlug($slug);
        if (!$artist) {
            abort(404);
        }
        return $this->show($artist->id);
    }

That's pretty much it.

Last updated 9 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

ottz0 ottz0 Joined 15 Nov 2014

Moderators

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

Your logo here?

Laravel.io

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

© 2024 Laravel.io - All rights reserved.