take a look at this: https://laracasts.com/series/laravel-5-from-scratch/episodes/8
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?
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.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community