Cannot Update my data from my edit from

Published 5 months ago by towhid

This is my edit form

<form class="col-md-6" method="PUT" action="/posts">
          <div class="form-group">
            <label for="title">Title</label>
            <input type="text" class="form-control" id="title" name="title" value="{{$post->title}}">
          </div>
          <div class="form-group">
            <label for="body">Body</label>
            <textarea class="form-control" rows="5" name="body">{{$post->body}}</textarea>
          </div>
          
          <button type="submit" class="btn btn-success">Update</button>
        </form>

This is my edit and update controller code

   public function edit($id)
    {
        $post=Post::find($id);
        return view('post.edit',compact('post'));
    }

    public function update(Request $request, $id)
    {
        $post=Post::find($id);
        $post->title = $request->input('title');
        $post->body = $request->input('body');
        $post->save();
        return redirect('/posts')->with('success','Post Updated');
    }

this is my model code

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    //
}

this is my route code


Route::resource('posts','postController');

but not work -- please -- tell me- the - probelm

Best Answer (As Selected By towhid)
rin4ik

@towhid

<form class="col-md-6" method="POST" action="{{route('posts.update',$post->id)}}">
    @method('PUT')
@csrf
bashy
bashy
5 months ago (1,002,600 XP)

Change

<form class="col-md-6" method="PUT" action="/posts">

to

<form class="col-md-6" method="POST" action="/posts">
    @method('PUT')

or

<form class="col-md-6" method="POST" action="/posts">
    <input type="hidden" name="_method" value="PUT">

Ref: https://laravel.com/docs/5.6/routing#form-method-spoofing

jlrdw
jlrdw
5 months ago (251,870 XP)

Put in the columns that's editable in model.

towhid

HEllo @bashy Not work -- this error show

Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException

No message

jlrdw
jlrdw
5 months ago (251,870 XP)
bashy
bashy
5 months ago (1,002,600 XP)

@jlrdw He's not mass assigning, so that shouldn't be an issue?

@towhid Need more info really. A network request of this would be handy to see what you're sending to what URI.

Cronix
Cronix
5 months ago (783,420 XP)

@jlrdw that does not apply here, especially due to the way he's saving.

@towhid the Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException is always due to sending the wrong type of request to an endpoint (route). Like you are sending a PUT request to a Route::get(), or something.

Since you're using PUT, you need a

Route::put('/posts', '[email protected]');
towhid

@jlrdw use your suggesion like ,

    protected $fillable = [
        'title', 'body'
    ];

but not work still same error

Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException No message

rin4ik
rin4ik
5 months ago (266,140 XP)

try this

<form class="col-md-6" method="POST" action="{{route('posts.update',$post->id)}}">
    @method('PUT')
towhid

@Cronix this is my route link

Route::resource('posts','postController');
Cronix
Cronix
5 months ago (783,420 XP)

Also, you need to add the id of the post you're editing in the form action.

<form class="col-md-6" method="post" action="/posts/{{ $post->id }}">

which is why it's going to the wrong endpoint. You probably don't need that route I suggested earlier since it should be covered by the Route::resource() route, but you do need the id of the post that's getting updated.

See the chart here on how resource controllers are layed out, the http verbs associated with the actions, and the parameters that each requires: https://laravel.com/docs/5.6/controllers#resource-controllers

towhid

@rin4ik shoe this error when iam trying to click edit button

ErrorException (E_ERROR)
Missing required parameters for [Route: posts.update] [URI: posts/{post}]. (View: C:\project\temp\resources\views\post\edit.blade.php)
jlrdw
jlrdw
5 months ago (251,870 XP)

Clear browser cache and temp view files in between each changing code.

rin4ik
rin4ik
5 months ago (266,140 XP)

@towhid I updated reply

<form class="col-md-6" method="POST" action="{{route('posts.update',$post->id)}}">
    @method('PUT')
towhid

@rin4ik this is message show

@method('PUT')
The page has expired due to inactivity. 

Please refresh and try again.
Vilfago

parameters should be in an array.

from rin4ik :

<form class="col-md-6" method="POST" action="{{route('posts.update',['post' =>$post->id])}}">
    @method('PUT')

Please sign in or create an account to participate in this conversation.