学習記録
2020-07-27に更新

Laravel From Scratch

Consider Named Routes

https://laracasts.com/series/laravel-6-from-scratch/episodes/28

routeのパスが変更されても編集が少なく済むように、named routeを使う。
named routeを使うには下記の記述を用いる。
- 定義はRoute::get('/hoge', 'hoge@hoge')->name('articles.show')
- パスを参照する時はroute('articles.show', $article)
- 上記の$articleのように「->id」を省略することもできる。

Reduce Duplication

https://laracasts.com/series/laravel-6-from-scratch/episodes/27

モデルの保存処理を短縮して書くには、createメソッドが利用できる。

Article::create(
    request()->validate([
            'title' => 'required',
            'excerpt' => 'required',
            'body' => 'required'
        ])
);

このままブラウザで登録処理を行うと、「allow mass assignment」というエラーが出る。
これを許可する方法として、モデルにfillable許可リストを書く方法とguardableブロックリストを書く方法がある。


dychw
学習記録
所有者限定モードのためこのボードには投稿できません
コメント
学習記録