1
votes

I am using Laravel 6 to make an edit form. Apparently, this is a prevalent problem, and I looked up here how can I solve it, I tried putting hidden csrf fields in 5 ways and I run with the same error every time, so IDK if those solutions are deprecated for Laravel 6 or I am doing something wrong.

edit.blade.php

<form method="POST" action="/posts/{{$post->edit}}" enctype="multipart/form-data">
    <input type="hidden" name="_method" value="PUT">
    <input type="hidden" name="_token" value="{{ csrf_token() }}">
    <div class="form-group">
        <label for="exampleFormControlInput1">Email address</label>
        <input type="email" name="email" value="{{ $post->email }}" class="form-control"
               id="exampleFormControlInput1">
    </div>
    <div class="form-group">
        <label for="exampleFormControlInput1">Name</label>
        <input type="text" name="name" value="{{ $post->name }}" class="form-control"
               id="exampleFormControlInput2" placeholder="Name">
    </div>
    <label for="exampleFormControlInput1">Image</label>
    <div class="form-group row">
        <div class="col-sm-2">
            @if($post->image)
                <img class="img-fluid card-img-top" src="/images/{{ $post->image }}"/>
            @endif
        </div>
        <input type="file" name="image" value="{{ $post->image }}"
               id="exampleFormControlInput3">
    </div>
    <button type="submit" class="btn btn-primary">Submit</button>
</form>

PostsController.php

    public function edit(Post $post)
    {
        return view ('posts.edit', compact('post'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Post $post)
    {
        $post->update($request->all());
        $post->name = $request->name;
        $post->email = $request->email;

        if(Input::hasFile('image')){
        $file = Input::file('image');
        $path = time().$file->getClientOriginalName();
        $destinationPath = public_path(). '/images/';
        $filename = time().$file->getClientOriginalName();
        $file->move($destinationPath, $filename);

       //then proceeded to save
       $post->image = $destinationPath.$filename;
       $post->save();
       }

       else
        $post->save();

    return redirect('posts.all');
    }

My routes, in case it's necessary

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

These are the other ways I tried to write the csrf field.

Way 1:

{{ csrf_field() }}
<input type="hidden" name="_method" value="PUT">

Way 2:

@csrf_field
{{ method_field('PUT') }}

Way 3:

@csrf
{{ method_field('PATCH') }}

Way 4:

@csrf
@method('PUT')

All of these lead me to the same error message.

1
@CaddyDZ I checked that specific question before posting this, nothing worked for meFlowMafia

1 Answers

2
votes

try to replace this :

<form method="POST" action="/posts/{{$post->edit}}" 

with this :

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

I think that you are trying to Post on the /posts/ route without any arguments as $post->edit might not return the id of the post :)