実現したいこと
breezeのregisterに自前で作ったテーブルの項目をリストボックスを追加したいです。
発生している問題・分からないこと
初学者なもので、どう書いて良いかわからないのですが、調べたところ、
1.Modelを作成する
2.ControllerでViewに渡す
3.Viewでforeachで回す
という流れだと思うのですが、Viewを開くとforeachしようとしているコレクション?の変数が宣言されていないというエラーになります。
「Undefined variable $workplaces」となります。
該当のソースコード
```Workplace.php <?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Workplace extends Model { use HasFactory; protected $fillable = [ 'name', ]; }
WorkplaceController.php
1<?php 2 3namespace App\Http\Controllers; 4 5use App\Models\Workplace; 6use Illuminate\Http\Request; 7 8class WorkplaceController extends Controller 9{ 10 public function getWorkplaces() 11 { 12 $workplaces = Workplace::all(); // 他のテーブルからデータを取得 13 return view('profile.edit', compact('workplaces')); 14 } 15 16 /** 17 * Display a listing of the resource. 18 */ 19 public function index() 20 { 21 // 22 } 23 24 /** 25 * Show the form for creating a new resource. 26 */ 27 public function create() 28 { 29 // 30 } 31 32 /** 33 * Store a newly created resource in storage. 34 */ 35 public function store(Request $request) 36 { 37 // 38 } 39 40 /** 41 * Display the specified resource. 42 */ 43 public function show(Workplace $workplace) 44 { 45 // 46 } 47 48 /** 49 * Show the form for editing the specified resource. 50 */ 51 public function edit(Workplace $workplace) 52 { 53 // 54 } 55 56 /** 57 * Update the specified resource in storage. 58 */ 59 public function update(Request $request, Workplace $workplace) 60 { 61 // 62 } 63 64 /** 65 * Remove the specified resource from storage. 66 */ 67 public function destroy(Workplace $workplace) 68 { 69 // 70 } 71}
register.blade.phpの一部
1<div class="mt-4"> 2 <x-input-label for="workplace_id" :value="__('workplace_id')" /> 3 <select class="form-control" id="workplace_id" name="workplace_id"> 4 @foreach ($workplaces as $workplace) 5 <option value="{{ $workplace->workplace->id }}">{{ $workplace->name }}</option> 6 @endforeach 7 </select> 8 <x-input-error :messages="$errors->get('workplace_id')" class="mt-2" /> 9 </div>
web.php
1<?php 2 3use App\Http\Controllers\ProfileController; 4use Illuminate\Support\Facades\Route; 5use App\Http\Controllers\WorkplaceController; 6 7/* 8|-------------------------------------------------------------------------- 9| Web Routes 10|-------------------------------------------------------------------------- 11| 12| Here is where you can register web routes for your application. These 13| routes are loaded by the RouteServiceProvider and all of them will 14| be assigned to the "web" middleware group. Make something great! 15| 16*/ 17 18// Route::get('/', function () { 19// return view('welcome'); 20// }); 21Route::get('/', function () { 22 return view('auth.login'); 23}); 24 25Route::get('/dashboard', function () { 26 return view('dashboard'); 27})->middleware(['auth', 'verified'])->name('dashboard'); 28 29Route::middleware('auth')->group(function () { 30 Route::get('/profile', [ProfileController::class, 'edit'])->name('profile.edit'); 31 Route::patch('/profile', [ProfileController::class, 'update'])->name('profile.update'); 32 Route::delete('/profile', [ProfileController::class, 'destroy'])->name('profile.destroy'); 33}); 34 35require __DIR__.'/auth.php';
2024_02_14_125456_create_workplaces_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 public function up(): void 13 { 14 Schema::create('workplaces', function (Blueprint $table) { 15 $table->id(); 16 $table->string('name'); 17 $table->timestamps(); 18 }); 19 } 20 21 /** 22 * Reverse the migrations. 23 */ 24 public function down(): void 25 { 26 Schema::dropIfExists('workplaces'); 27 } 28};
試したこと・調べたこと
- teratailやGoogle等で検索した
- ソースコードを自分なりに変更した
- 知人に聞いた
- その他
上記の詳細・結果
chatGptやいろいろなサイトで調べたので、ここで調べたと出せません。すいません。
補足
一つ疑問があります。
Controllerに記述する際、Viewに返すと記載があるのですが、画面が登録、修正、削除とあった場合、それぞれにリストを変えすコードの記述が必要なのでしょうか?
AWS Lightsail上のLaravel10です。
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。