Laravelでモデルクラスを用いたDB検索処理をコーディングしているのですが
コントローラとblade.phpの受け渡しが上手くいかなくて困っています。※以下エラーが出ます。
ErrorException (E_ERROR) Undefined variable: items (View: /var/www/laravel/laravelapp/resources/views/person/find.blade.php)
・やりたいこと
⇒Personテーブルのname列を元にキーワード検索して複数の結果をforeachで表示させたい。
・コントローラ(抜粋) class PersonController extends Controller { public function find(Request $request) { return view('person.find', ['input' => '']); } public function search(Request $request) { $keyword = $request->input; $items = Person::where('name', $keyword)->get(); return view('person.find', ['input' => $keyword, 'items' => $items]); } }
・find.blade.php(抜粋) @section('content') <form action="/home/find" method="post"> {{ csrf_field() }} <input type="text" name="input" value="{{$input}}"> <input type="submit" value="検索"> </form> @if ($items != null) @foreach($items as $item) <table> <tr><th>Data</th></tr> <tr> <td>{{$item->name}}</td> </tr> </table> @endforeach @endif @endsection
配列の受け取り方でミスしている気もするのですが
詳しい方がいらっしゃいましたらよろしくお願いいたします。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。