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 site.dev/news/1 make site.dev/news/slug_url. And you can do it without using different packages?
web.php
Route::post('news','ArticlesController@store');
Route::resource('news',"ArticlesController",
 ['except' => ['show']]);
Route::get('news/{slug?}', 'ArticlesController@show');

ArticlesController
public function show($id)
{
 $article = Article::findOrFail($id);
 return view('articles.show', compact('article'));
}
 public function store(ArticleRequest $request)
{
 $input = $this->imageArticleRequest($request);
Auth::user()
->articles()
->create($input);
 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);
Image::make($image)
->save($savePath);
 $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
Solution
You need to read this section of the documentation: https://laravel.com/docs/5.5/routing

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

RouteServiceProvider:
public function boot()
{
parent::boot();

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

ArticlesController:
public function show(App\Article $article) {
//
}
Thank you.
public function show (Article $article) {
 return view('articles.show', 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
https://laravel.com/docs/5.5/routing#implicit-binding
public function getRouteKeyName()
{
 return 'slug';
}

Find more questions by tags Laravel