user979331
user979331

Reputation: 11851

Laravel - Edit and Update Page

I am using Laravel and I am trying to create an edit page and call my update method on submit, the problem is I am getting a 404 when updating. This is my blade file for editing like so:

@extends('adminlte::page')

@section('title', 'AdminLTE')

@section('content_header')
    <h1>Professions</h1>
@stop

@section('content')
    <form method="PUT" action="/admin/professions-update/{{ $data->pkprofession }}">
        <div class="form-group">
            <label for="profession_name">Profession Name</label>
            <input type="text" name="profession_name" id="profession_name" class="form-control" value="{{$data->profession_name}}" />
        </div>
        <div class="form-group">
            <button type="submit" class="btn btn-success">Update</button>
        </div>
    </form>
@stop

Here are my routes:

Route::get('/admin/professions-edit/{id}', 'v1\ProfessionsController@edit');
Route::put('/admin/professions-update/{id}', 'v1\ProfessionsController@update');

And Here are the methods being called:

public function edit($id)
    {
        $data = PdTprofession::find($id);
        return view('professions-edit', compact('data'));
    }

public function update(Request $request, $id)
    {
        $data = PdTprofession::find($id);
        return view('professions-edit', compact('data'));
    }

Why am I getting a 404 error and how do I fix it?

Thanks,

Upvotes: 4

Views: 13592

Answers (4)

BO PISEY
BO PISEY

Reputation: 1

Route::put('/your-model/{id}', [YourController::class, 'update'])->name('your-model.update');

Upvotes: -1

Mayank Pandeyz
Mayank Pandeyz

Reputation: 26258

There are so many issues in your code lets resolve one by one:

action="/admin/professions-update/{{ $data->pkprofession }}">

change it to:

action="{{ url('/admin/professions-update/' . $data->pkprofession) }}">

and then HTML forms do not support PUT, PATCH or DELETE actions, so chage it to:

<form action="{{ url('/admin/professions-update/' . $data->pkprofession) }}" method="POST">
    @method('PUT')
    @csrf  // this is required when you are using the method other then 'get'
    other elements
</form>

Upvotes: 2

Piazzi
Piazzi

Reputation: 2636

You're missing the csrf token and the method input. Try this:


@extends('adminlte::page')

@section('title', 'AdminLTE')

@section('content_header')
    <h1>Professions</h1>
@stop

@section('content')
    <form method="POST" action="/admin/professions-update/{{ $data->pkprofession }}">
        @csrf
        @method('PUT')
        <div class="form-group">
            <label for="profession_name">Profession Name</label>
            <input type="text" name="profession_name" id="profession_name" class="form-control" value="{{$data->profession_name}}" />
        </div>
        <div class="form-group">
            <button type="submit" class="btn btn-success">Update</button>
        </div>
    </form>
@stop

Also, in your update method you are forgeting to update the object, add this to your code:

$data->update($request->all());

For more info: DOCS

Upvotes: 2

Rahul
Rahul

Reputation: 18557

In laravel docs, HTML forms do not support PUT, PATCH or DELETE actions. So, when defining PUT, PATCH or DELETE routes that are called from an HTML form, you will need to add a hidden _method field to the form. The value sent with the _method field will be used as the HTTP request method:

<form action="/foo/bar" method="POST">
    <input type="hidden" name="_method" value="PUT">
    <input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>

You may use the @method Blade directive to generate the _method input:

<form action="/foo/bar" method="POST">
    @method('PUT')
    @csrf
</form>

Upvotes: 5

Related Questions