前提・実現したいこと
現在laravelの教本通りに進めており、DBクラスを使って指定されたwebサイトにアクセスした際にエラーが出てしまうのでこのエラーを改善したいです。
発生している問題
laravelを使ってDBクラスを利用する際、未定義の項目としてUndefined variable: itemsのエラーが表示されてしまうのでもし、この改善方法をわかる方がいらっしゃいましたらご助言お願いできればと思います。
該当のソースコード
"Undefined variable: items (View: C:\Users\n\Desktop\laravelapp\resources\views\hello\index.blade.php)"
試したこと
index.blade.phpのファイルの中身の@section('content')を教本通り下記に変更しました。
html
1@section('content') 2 <table> 3 <tr><th>Name</th><th>Mail</th><th>Age</th></tr> 4 @foreach ($items as $item) 5 <tr> 6 <td>{{$item->name}}</td> 7 <td>{{$item->mail}}</td> 8 <td>{{$item->age}}</td> 9 </tr> 10 @endforeach 11 </table> 12@endsection 13
補足情報(FW/ツールのバージョンなど)
アクセスしたいURLはhttp://localhost:8000/helloとなります。
controller
1<?php 2 3namespace App\Http\Controllers; 4 5use Illuminate\Http\Request; 6use Illuminate\Http\Response; 7use Illuminate\Support\Facades\DB; 8 9class HelloController extends Controller 10{ 11 12 13public function index(Request $request) 14{ 15 $items = DB::select('select * from people'); 16 return view('hello.index', ['items' => $items]); 17} 18}
routes
1<?php 2Route::get('/', function () { 3 return view('welcome'); 4}); 5Route::get('hello', function() { 6 return view('hello.index'); 7});
ここにより詳細な情報を記載してください。
回答2件
あなたの回答
tips
プレビュー