Help us understand the problem. What is going on with this article?

Laravel 編集機能を使った時に行が1行増えてしまう問題について

はじめに

Laravelの~Controller@updateを使用した時に
編集した値が編集前のデータと編集後のデータの2つが登録されて
しまいにっちもさっちも行かない方向けの記事になります。

以下では、ユーザーが書いた記事を編集するというプロジェクトについてのcontroller,viewになります。

コード

controller

PostsController.php
public function update(Request $request, $post_id){
        $params = $request->validate([
            'title'=>'required|max:50',
            'body'=>'required|max:2000',
        ]);
        $post = Post::findOrFail($post_id);
        $post->fill($params)->save();

        return redirect()->route('posts.show',['post'=>$post]);
    }

view

view/edit.blade.php
    <div class="container mt-4">
        <div class="border p-4">
            <h1 class="h5 mb-4">投稿の編集</h1>
        </div>
        <form action="{{route('posts.update',['post'=>$post])}}"method="POST">
            @csrf
            @method('PUT')
            <fieldset class="mb-4">
                <div class="form-group">
                    <label for="title">タイトル</label>
                    <input type="text"id="title"name="title"class="form-control {{ $errors->has('title')?'is-invalid':''}}"value="{{old('title')?:$post->title}}">
                    @if($errors->has('title'))
                        <div class="invalid-feedback">
                            {{$errors->first('title')}}
                        </div>
                    @endif
                </div>
                <div class="form-group">
                    <label for="body">本文</label>
                    <textarea name="body" id="body" class="form-control {{$errors->has('body')?'is-invalid':''}}" rows="4">
                        {{old('body')?:$post->body}}
                    </textarea>
                    @if($errors->has('body'))
                        <div class="invalid-feedback">
                            {{$errors->first('body')}}
                        </div>
                    @endif

                    <div class="mt-5">
                        <a class="btn btn-primary"href="{{route('posts.show',['post'=>$post])}}">キャンセル</a>
                        <button type="submit"class="btn btn-primary">更新する</button>
                    </div>
                </div>

            </fieldset>

@method('PUT')を入れるのがポイントです。

routing

ちなみにroutes/web.phpは

routeweb.php

Route::resource('posts', 'PostsController', ['only' => ['create', 'store', 'show','edit','update','destroy']]);

$ php artisan route:list
+--------+-----------+-------------------+----------------+-----------------------------------------------+--------------+
| Domain | Method    | URI               | Name           | Action                                        | Middleware   |
+--------+-----------+-------------------+----------------+-----------------------------------------------+--------------+
|        | GET|HEAD  | /                 | top            | App\Http\Controllers\PostsController@index    | web          |
|        | GET|HEAD  | api/user          |                | Closure                                       | api,auth:api |
|        | POST      | comments          | comments.store | App\Http\Controllers\CommentsController@store | web          |
|        | POST      | posts             | posts.store    | App\Http\Controllers\PostsController@store    | web          |
|        | GET|HEAD  | posts/create      | posts.create   | App\Http\Controllers\PostsController@create   | web          |
|        | GET|HEAD  | posts/{post}      | posts.show     | App\Http\Controllers\PostsController@show     | web          |
|        | PUT|PATCH | posts/{post}      | posts.update   | App\Http\Controllers\PostsController@update   | web          |
|        | DELETE    | posts/{post}      | posts.destroy  | App\Http\Controllers\PostsController@destroy  | web          |
|        | GET|HEAD  | posts/{post}/edit | posts.edit     | App\Http\Controllers\PostsController@edit     | web          |
+--------+-----------+-------------------+----------------+-----------------------------------------------+--------------+

こうなっています。

@method('PUT')を入れるか入れないかで、その後のデータが辿る経路が変わってしまうとは。。。

tnatsume00
都内のWeb制作会社でバックフロントエンジニアとして働いています。 現在、人づてに聞いたreactnative,expoにはまっていて、色々と触って、 その中での気づきなどを記事にしています。
https://github.com/tnatsume?tab=repositories
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした