実現したいこと
Laravelのcrudでデーターベースの編集と更新をしたいです。
発生している問題・分からないこと
formタグを使い更新ボタンを押下後、updateの処理をしたいが送られてくる値が全てnullになります。
値の送り方が間違っており、文字列がnull判定になっているのでしょうか?
もしくは値の受け取り方が間違っており、null判定になっているのでしょうか?
丸投げのような質問になり申し訳ございません。
エラーも出ず原因特定に躓いております。
ヒントだけでもいただけたら幸いです。
該当のソースコード
PHP
1Route::get('/players/{id}', [PlayersController::class, 'show'])->middleware('RedirectIfDirectAccess'); 2Route::get('/players', [PlayersController::class, 'index'])->name('players.index'); 3 4Route::get('/players/{id}/delete', [PlayersController::class, 'logicalDelete']); 5Route::get('/players/edit/{id}', [PlayersController::class, 'edit2'])->middleware('RedirectIfDirectAccess')->name('players.edit2'); 6 7Route::post('/players/{id}/update', [PlayersController::class, 'update'])->name('players.update');
PHP(Controller)
1 public function update(Request $request, $playerId) 2 { 3 $player = Player::findOrFail($playerId); 4 $player->uniform_num = $request->uniform_num; 5 $player->uniform_num = $request->input; 6 $player->position = $request->position; 7 $player->name = $request->player_name; 8 $player->club = $request->club; 9 $player->birth = $request->birth; 10 $player->height = $request->height; 11 $player->weight = $request->weight; 12 $player->save(); 13 14 return redirect()->route('players.index', $player)->with('success', '選手情報が更新されました。'); 15 }
HTML
1<!DOCTYPE html> 2<html lang="ja"> 3<head> 4 <meta charset="UTF-8"> 5 <meta name="viewport" content="width=device-width, initial-scale=1.0"> 6 <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet"> 7 <link href="/css/bootstrap.min.css" rel="stylesheet"> 8 <link href="/css/world.css" rel="stylesheet"> 9 10 <title>選手1編集画面</title> 11</head> 12<body> 13 <div class="player-show world"> 14 <form action="{{ route('players.update', $player->id) }}" method="POST"> 15 @csrf 16 <!-- @method('put') --> 17 <input type="hidden" name="id" value="{{ $player->id }}"> 18 <table class="table table-striped"> 19 <tr> 20 <th>No</th> 21 <td>{{ $player->id }}</td> 22 </tr> 23 <tr> 24 <th>背番号</th> 25 <td><input type="text" name="uniform_num" value="{{ $player->uniform_num }}"></td> 26 </tr> 27 <tr> 28 <th>ポジション</th> 29 <td> 30 <select name="position"> 31 @foreach($positions as $position) 32 <option value="{{ $position }}" @if($position == $player->position) selected @endif>{{ $position }}</option> 33 @endforeach 34 </select> 35 </td> 36 </tr> 37 <tr> 38 <th>名前</th> 39 <td><input type="text" name="player_name" value="{{ $player->player_name }}"></td> 40 </tr> 41 <tr> 42 <th>国</th> 43 <td> 44 <select name="country_id"> 45 @foreach($countries as $countryId => $countryName) 46 <option value="{{ $countryId }}" @if($countryId == $player->country_id) selected @endif>{{ $countryName }}</option> 47 @endforeach 48 </select> 49 </td> 50 </tr> 51 <tr> 52 <th>所属</th> 53 <td><input type="text" name="club" value="{{ $player->club }}"></td> 54 </tr> 55 <tr> 56 <th>誕生日</th> 57 <td><input type="date" name="birth" value="{{ $player->birth }}"></td> 58 </tr> 59 <tr> 60 <th>身長</th> 61 <td><input type="number" name="height" value="{{ $player->height }}"></td> 62 </tr> 63 <tr> 64 <th>体重</th> 65 <td><input type="number" name="weight" value="{{ $player->weight }}"></td> 66 </tr> 67 </table> 68 <button type="submit">更新</button> 69</form> 70 <h5 class="return"><a href="{{ route('players.index') }}" class="a-return">戻る</a></h5> 71 </div> 72</body> 73</html>
試したこと・調べたこと
- teratailやGoogle等で検索した
- ソースコードを自分なりに変更した
- 知人に聞いた
- その他
上記の詳細・結果
dd($player)やdd($request)などで確認をしましたが、値がnullになります。
補足
特になし

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2024/03/13 23:33
2024/03/14 02:08