Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel - Edit and Update Page

Tags:

php

laravel

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,

like image 397
user979331 Avatar asked Nov 04 '25 06:11

user979331


1 Answers

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>
like image 100
Rahul Avatar answered Nov 06 '25 22:11

Rahul



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!