Tự học Laravel: (32) ROUTE MODEL BINDING

This time, I would like to modify the controller using Route Model Binding function. Route Model Binding is a convenient function that can receive an instance of a model corresponding to an id instead of an id where a model id has been received so far.

ROUTE CONFIRMATION

php artisan route:list

+ -------- + ----------- + ------------------------- + - ---------------- + --------------------------------- --------------------------------------- + ---------- ---- +
| Domain | Method    | URI                     | Name             | Action                                                                 | Middleware   |
|        | DELETE    | articles/{article}      | articles.destroy | App\Http\Controllers\ArticlesController@destroy                        | web,auth     |
|        | PUT|PATCH | articles/{article}      | articles.update  | App\Http\Controllers\ArticlesController@update                         | web,auth     |
|        | GET|HEAD  | articles/{article}      | articles.show    | App\Http\Controllers\ArticlesController@show                           | web          |
|        | GET|HEAD  | articles/{article}/edit | articles.edit    | App\Http\Controllers\ArticlesController@edit                           | web,auth     |
+ -------- + ----------- + ------------------------- + - ---------------- + --------------------------------- --------------------------------------- + ---------- ---- +

There are four actions that have received the {article} parameter.

  • articles.show
  • articles.edit
  • articles.update
  • articles.destroy

The controller method of these routes is performing the process of searching for Article by id.

$article = Article::findOrFail($id);
// app/Http/Controllers/ArticlesController.php
 
...
 
class ArticlesController extends Controller {
 
    ...
 
    public function show($id) {
        $article = Article::findOrFail($id); 
 
        return view('articles.show', compact('article'));
    }
 
    ...
 
    public function edit($id) {
        $article = Article::findOrFail($id);
 
        return view('articles.edit', compact('article'));
    }
 
    public function update(ArticleRequest $request, $id) {
        $article = Article::findOrFail($id);
 
        $article->update($request->validated());
 
        return redirect()->route('articles.show', [$article->id])
            ->with('message', 'Update Ok');
    }
 
    public function destroy($id) {
        $article = Article::findOrFail($id); 
 
        $article->delete();
 
        return redirect()->route('articles.index')
            ->with('message', 'Delete Ok');
    }
}

Using Route Model Binding eliminates the need for the controller to perform this search process.

ROUTE MODEL BINDING

Modify ArticlesController.php

//app/Http/Controllers/ArticlesController.php
 
...
 
class ArticlesController extends Controller {
 
    ...
 
    public function show(Article $article) { 
        return view('articles.show', compact('article'));
    }
 
    ...
 
    public function edit(Article $article) {
        return view('articles.edit', compact('article'));
    }
 
    public function update(ArticleRequest $request, Article $article) { 
        $article->update($request->validated());
 
        return redirect()->route('articles.show', [$article->id])
            ->with('message', 'Update Ok');
    }
 
    public function destroy(Article $article) {
        $article->delete();
 
        return redirect()->route('articles.index')
            ->with('message', 'Delete Ok');
    }
}

So far, I changed the four methods that received $id as arguments to receive Article $article.
Article::findOrFail($id); has deleted the process of searching for articles from $id.

Astonishingly, Article itself is automatically passed to the $article variable that matches the id specified for the {article} part of the route. This feature is called Route Model Bindeing.

The root {article} parameter and the controller $article argument must have the same name.

I thought that the previous controller was simple enough, but it is even simpler and super cool.

Official site 
http://laravel.com/docs/master/routing#route-model-binding

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.