Controllerは、php artisan make:controller で作成する。名前は単数形。
\App\Http\Controllersに作成される。
URLとメソッドで指定されるアクションで処理を実行する。
RESTfulに作成する場合は、--resourceオプションをつける。
php\artisan make:controller HogehogeController –resource
index, create, store, edit, update, show, destroyのアクションが自動的に用意される。
storeは保存、updtateは更新、destroyは削除を行い、指定のURLへリダイレクトする。
その他のアクションでは、表示画面すなわちviewを指定する。
(viewはbladeファイルで、\resources\viewsフォルダーに保存する。)
(bladeファイルは名称.blade.phpというファイル名)
指定の仕方はviewsフォルダー内のフォルダー名(基本複数形)、ファイル名称。
\resources\views\news\index.blade.phpの場合、news.indexとなる。
viewにオブジェクトを渡す場合に、PHPのcompact関数が便利。
リレーショナルの親テーブル(authority)の情報をauthority_rollのviewに渡す例。
public function edit($id)
{
$authorities = Authority::where('id','>', '1')->get()->sortBy("sort_no");
$item = Authority_Roll::find($id);
return view('authority_rolls.edit',compact('item','authorities'));
}