LoginSignup
yuktkhs
@yuktkhs (yuki takahashi)

Are you sure you want to delete the question?

Leaving a resolved question undeleted may help others!

Laravelで編集画面を表示させたい

Q&AClosed

解決したいこと

画像付きレシピ投稿アプリを作成しています。想定している画面の遷移は以下の通りです。
レシピ一覧画面(/home)

レシピ詳細画面(detail.blade.php)

(編集ボタンを押す)
レシピ編集画面(edit.blade.php)

レシピ編集画面に遷移させる際にエラーが出てしまい、解決方法がわからずにいます。
ご教示いただけますと幸いでございます。

発生している問題・エラー

Missing required parameter for [Route: ] [URI: recipe/edit/{id}] [Missing parameter: id]. 

該当するソースコード

詳細画面(detail.blade.php)

特定のデータを$postに入っています。
dd($post->id)でIDが取得できていることを確認しました

<a href="/recipe/edit/{{ $post->id }}">編集</a>

ルーティング(web.php)

web.php
// レシピ編集画面を表示
Route::get('recipe/edit/{id}', 'App\Http\Controllers\RecipeController@edit');
Route::post('recipe/edit/{id}', 'App\Http\Controllers\RecipeController@update');

コントローラー(RecipeController)

$idでURLからパラメータを受け取る
edit.blade.phpにデータを渡す

web.php
// レシピ編集画面を表示する
    public function edit($id){

        $post = Recipe::find($id);
        // dd($post);
        return view('edit', ['post' => $post]);
    }

編集画面(edit.blade.php)

div class="container">
        <div class="row">
            <div class="col-md-8 mx-auto">
                <h2 class="text-center">レシピ編集</h2>
                <form action="{{ action('App\Http\Controllers\RecipeController@edit') }}" method="post" enctype="multipart/form-data">
                    @if (count($errors) > 0)
                        <ul>
                            @foreach($errors->all() as $e)
                                <li>{{ $e }}</li>
                            @endforeach
                        </ul>
                    @endif


                    <div class="form-group row">
                        <label class="col-md-2" for="title">レシピ名</label>
                        <div class="col-md-10">
                            <input type="text" class="form-control" name="recipe_name" value="{{ $post->recipe_name }}">
                        </div>
                    </div>
                    <div class="form-group row">
                        <label class="col-md-2" for="title">ジャンル</label>
                        <div class="col-md-10">
                            <input type="text" class="form-control" name="tag_name" value="{{ $post->tag_name }}">
                        </div>
                    </div>
                    <div class="form-group row">
                        <label class="col-md-2" for="body">コメント</label>
                        <div class="col-md-10">
                            <textarea class="form-control" name="comment" rows="20">{{ $post->comment }}</textarea>
                        </div>
                    </div>
                    <div class="form-group row">
                        <label class="col-md-2" for="title">画像</label>
                        <div class="col-md-10">
                            <input type="file" class="form-control-file" name="image">
                        </div>
                        <div>
                            設定中 {{ $post->image_path }}
                        </div>
                    </div>
                    {{ csrf_field() }}
                    <input type="submit" class="btn btn-primary" value="更新">

                </form>
            </div>
        </div>
    </div>

編集画面でデバッグしてみました
dd($post)
image.png

0

1Answer

エラーが出ているのはaction()の箇所ではないでしょうか?
ルーティングを見るとidが必要ですが指定されていません。

edit.blade.php
<form action="{{ action('App\Http\Controllers\RecipeController@edit') }}" method="post" enctype="multipart/form-data">

さらに言うとPOSTリクエストなので、@editではなく@updateではないでしょうか?

0

Comments

  1. @yuktkhs

    Questioner
    丁寧なご回答ありがとうございます。
    ご指摘の通りでした。

Your answer might help someone💌