0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

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

Posted at

#はじめに

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')を入れるか入れないかで、その後のデータが辿る経路が変わってしまうとは。。。

0
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?