Q&A
実現したいこと
- データーベースに登録した値をブラウザ上に表示させたい
前提
https://b-risk.jp/blog/2022/08/laravel/
PHPを使って上記サイトを参考にアプリ作成の練習をしています
データーベースに、テストデータを挿入し、モデルとコントローラーとviewを簡易的に作成したのですが、値がうまく引き渡せていないためか、エラーが表示されてしまいました。
発生している問題・エラーメッセージ
Undefined variable $todo_lists
該当のソースコード
todo_list\index.blade.php
1 2<!DOCTYPE html> 3 4<html lang="ja"> 5 6 7 8<head> 9 10 <meta charset="UTF-8"> 11 12 <meta name="viewport" content="width=device-width, initial-scale=1.0"> 13 14 <meta http-equiv="X-UA-Compatible" content="ie=edge"> 15 16 <title>テスト</title> 17 18</head> 19 20 21 22<body> 23 24 25 26 @if ($todo_lists->isNotEmpty()) 27 28 <ul> 29 30 @foreach ($todo_lists as $item) 31 32 <li> 33 34 {{ $item->name }} 35 36 </li> 37 38 @endforeach 39 40 </ul> 41 42 @endif 43 44 45 46</body> 47 48 49 50</html>
試したこと
if(isset($変数名)) に変更してみましたが、別のエラーが出てしまい、解決につながりませんでした。
補足情報(FW/ツールのバージョンなど)
MAMPでWebサーバー構築
windows11/VScode
Composer version 2.5.3
PHP version 8.1.0
create_todo_lists_table.php
1<?php 2 3use Illuminate\Database\Migrations\Migration; 4use Illuminate\Database\Schema\Blueprint; 5use Illuminate\Support\Facades\Schema; 6 7return new class extends Migration 8{ 9 /** 10 * Run the migrations. 11 * 12 * @return void 13 */ 14 public function up() 15 { 16 Schema::create('todo_lists', function (Blueprint $table) { 17 $table->id(); 18 $table->string('name', 100); 19 $table->timestamps(); 20 }); 21 } 22 23 /** 24 * Reverse the migrations. 25 * 26 * @return void 27 */ 28 public function down() 29 { 30 Schema::dropIfExists('todo_lists'); 31 } 32}; 33
TodoListSeeder.php
1<?php 2 3namespace Database\Seeders; 4 5use Illuminate\Database\Console\Seeds\WithoutModelEvents; 6use Illuminate\Database\Seeder; 7use Illuminate\Support\Facades\DB; 8 9class TodoListSeeder extends Seeder 10{ 11 /** 12 * Run the database seeds. 13 * 14 * @return void 15 */ 16 public function run() 17 { 18 DB::table('todo_lists')->insert( 19 [ 20 [ 21 'name' => 'テスト1', 22 'created_at' => now(), 23 'updated_at' => now(), 24 ], 25 [ 26 'name' => 'テスト2', 27 'created_at' => now(), 28 'updated_at' => now(), 29 ], 30 [ 31 'name' => 'テスト3', 32 'created_at' => now(), 33 'updated_at' => now(), 34 ], 35 ] 36 ); 37 } 38}
DatabaseSeeder.php
1<?php 2 3namespace Database\Seeders; 4 5// use Illuminate\Database\Console\Seeds\WithoutModelEvents; 6use Illuminate\Database\Seeder; 7 8class DatabaseSeeder extends Seeder 9{ 10 /** 11 * Seed the application's database. 12 * 13 * @return void 14 */ 15 public function run() 16 { 17 $this->call([ 18 TodoListSeeder::class 19 ]); 20 } 21}
TodoListController.php
1<?php 2 3namespace App\Http\Controllers; 4 5use Illuminate\Http\Request; 6use App\Models\TodoList; 7 8class TodoListController extends Controller 9{ 10 public function index(Request $request) 11 { 12 $todo_lists = TodoList::all(); 13 14 return view('todo_list.index', ['todo_lists', $todo_lists]); 15 } 16}
web.php
1<?php 2 3use Illuminate\Support\Facades\Route; 4use App\Http\Controllers\TodoListController; 5/* 6|-------------------------------------------------------------------------- 7| Web Routes 8|-------------------------------------------------------------------------- 9| 10| Here is where you can register web routes for your application. These 11| routes are loaded by the RouteServiceProvider within a group which 12| contains the "web" middleware group. Now create something great! 13| 14*/ 15Route::get('/list', [TodoListController::class, 'index']); 16Route::get('/', function () { 17 return view('welcome'); 18}); 19
回答1件
あなたの回答
tips
プレビュー
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。