How to display the slug instead of id in URL?

Hello! There are articles, to each of them, I formed a slug and recorded in the database using str_slug(). How is make And you can do it without using different packages?
 ['except' => ['show']]);
Route::get('news/{slug?}', 'ArticlesController@show');

public function show($id)
 $article = Article::findOrFail($id);
 return view('', compact('article'));
 public function store(ArticleRequest $request)
 $input = $this->imageArticleRequest($request);
 session()->flash('flash_message', 'Article created successfully');
 return redirect('news');
 protected function imageArticleRequest($request)
 if ($request->hasFile('article_wall')) {
 $image = $request->file('article_wall');
 $imageName = time() . "." . $image->getClientOriginalExtension();
 $savePath = public_path('/uploads/articleImages/' . $imageName);
 $input = $request->all();
 $input = array_except($input, 'pathName');
 $input['article_wall'] = $imageName;
 $title = $input['title'];
 $input['slug'] = str_slug($title);
 return $input;
June 14th 19 at 18:10
2 answers
June 14th 19 at 18:12
You need to read this section of the documentation:

Route::get('news/{article}', 'ArticlesController@show');

public function boot()

 Route::model('article', App\Article::class);

public function show(App\Article $article) {
Thank you.
public function show (Article $article) {
 return view('', compact('article'));

and in view caused
<a href="{{ url('/news', $article->slug) }}">{{ $article->title }}</a>

However, there is a question. There is a package on github that implements this feature. He is better than this method? And can it be used in production? - Eryn8 commented on June 14th 19 at 18:15
what kind of package it?

And there is a matter of taste, but a link is better to form this:
{{ action('ArticlesController@show', ['article' => $article]) }}

Much easier to find when refactoring. And if you use an IDE with support for L5, it will work auto-completion and other buns. - felipa29 commented on June 14th 19 at 18:18
he seems to be one.
cviebrock/eloquent-sluggable - Eryn8 commented on June 14th 19 at 18:21
judging by the sources he does everything the same as I wrote above. Want use it I want something that is higher. And even better to understand what is happening in principle. - felipa29 commented on June 14th 19 at 18:24
June 14th 19 at 18:14
public function getRouteKeyName()
 return 'slug';

Find more questions by tags Laravel