前提
Laravelで一覧画面を表示している状況で、ラジオボタンを選択して選択したレコードを更新ボタンを押すことで、選択したレコードを次の画面で表示させたいのですが、選択したレコードのデータを渡せません。
最下部に記載していますが、dd($request)で送信されたrequestデータを確認しましたが、
id:2を選択したはずが、3のデータがrequestされているようです。なぜでしょうか?
todo.blade.phpの書き方が悪く、MainController.phpに値を渡せていないと思いますが、どのようにすれば、
解消できるのかわかりません。
どなたか対処法をご存知の方がいればご連絡お願いします。
発生している問題・エラーメッセージ
Attempt to read property "id" on array
###MainController.php
<?php namespace App\Http\Controllers; use Illuminate\Support\Facades\DB; use Illuminate\Http\Request; class MainController extends Controller { public function index() { $items = DB::select('select * from todoList order by id'); return view('todo', ['items' => $items]); } public function changeInput(Request $request) { // dd($request); $param = ['id' => $request->todoId]; // dd($param); $item = DB::select('select * from todoList where id = :id', $param); return view('changeInput', ['item' => $item]); } }
###todo.blade.php
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <link rel="stylesheet" href="main.css"> <title>初期表示一覧画面</title> <script> //a勤務表編集選択画面でラジオボタン選択中は変更削除ボタンを有効にする //aラジオボタンを選択していないときは無効にする function check(){ var flag = true; // 選択されているか否かを判定するフラグ //a ラジオボタンの数だけ判定を繰り返す(ボタンを表すインプットタグがあるので1引く) for(var i=0; i<document.form.ids.length-1;i++){ // i番目のラジオボタンがチェックされているかを判定 if(document.form.ids[i].checked){ flag = true; document.getElementById("henko").disabled = false; document.getElementById("sakujyo").disabled = false; } } } </script> </head> <body> <h1>todoリスト</h1> <form name="form"> <table border="1"> @csrf <tr> <th>選択</th> <th>No.</th> <th>タスク</th> <th>優先度</th> <th>期日(日時)</th> <th>担当者</th> <th>備考</th> </tr> @foreach ($items as $item) <tr> <td><input type="radio" name="todoId" id="ids" value={{$item->id}} onClick="return check()"></td> <td><input type="text" name="todoId" value={{$item->id}} readonly></td> <td><input type="text" name="task" value={{$item->task}} readonly></td> <td><input type="text" name="priority" value={{$item->priority}} readonly></td> <td><input type="text" name="deadline" value={{$item->deadline}} readonly></td> <td><input type="text" name="manager" value={{$item->manager}} readonly></td> <td><input type="text" name="remarks" value={{$item->remarks}} readonly></td> </tr> @endforeach </table> <button type="submit" formaction="addInput" formmethod="get">新規</button><button type="submit" formaction="changeInput" formmethod="post" name="henko" id="henko" disabled>更新</button><button type="submit" formaction="deleteConfirm" formmethod="post" name="sakujyo" id="sakujyo" disabled>削除</button> </form> </body> </html>
###changeInput.blade.php
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <link rel="stylesheet" href="main.css"> <title>更新入力画面</title> </head> <body> <h1>todoリスト</h1> <form name="form"> <table border="1"> <tr><th>No.</th><td><input type="text" name="todoId" value={{$item->id}}></td></tr> <tr><th>タスク</th><td><input type="text" name="task" value={{$item->task}}></td></tr> <tr><th>優先度</th><td><input type="text" name="priority" value={{$item->priority}}></td></tr> <tr><th>期日(日時)</th><td><input type="text" name="deadline" value={{$item->deadline}}></td></tr> <tr><th>担当者</th><td><input type="text" name="manager" value={{$item->manager}}></td></tr> <tr><th>備考</th><td><input type="text" name="remarks" value={{$item->remarks}}></td></tr> </table> <button type="submit" formaction="changeConfirm" formmethod="post" name="henko" id="henko">更新確認</button> </form> </body> </html>
試したこと
dd($request)でリクエストされた入力値を表示
Illuminate\Http\Request {#44 ▼ // app/Http/Controllers/MainController.php:17 +attributes: Symfony\Component\HttpFoundation\ParameterBag {#46 ▶} +request: Symfony\Component\HttpFoundation\InputBag {#45 ▼ #parameters: array:8 [▼ "_token" => "cKodPBF6tdP4BPJKBjqpQEsuS68OipaHVFL9emVn" "todoId" => "3" "task" => "単体テスト" "priority" => "3" "deadline" => "2023-09-29" "manager" => "hashimoto" "remarks" => "取得元の確認" "henko" => null ] } +query: Symfony\Component\HttpFoundation\InputBag {#52 ▶} +server: Symfony\Component\HttpFoundation\ServerBag {#48 ▶} +files: Symfony\Component\HttpFoundation\FileBag {#49 ▶} +cookies: Symfony\Component\HttpFoundation\InputBag {#47 ▶} +headers: Symfony\Component\HttpFoundation\HeaderBag {#50 ▶} #content: null #languages: null #charsets: null #encodings: null #acceptableContentTypes: null #pathInfo: "/changeInput" #requestUri: "/changeInput" #baseUrl: "" #basePath: null #method: "POST" #format: null #session: Illuminate\Session\Store {#272 ▶} #locale: null #defaultLocale: "en" -preferredFormat: null -isHostValid: true -isForwardedValid: true #json: null #convertedFiles: null #userResolver: Closure($guard = null) {#238 ▶} #routeResolver: Closure() {#247 ▶} basePath: "" format: "html" }

バッドをするには、ログインかつ
こちらの条件を満たす必要があります。