Luzan Baral
Luzan Baral

Reputation: 3698

Patch route gives 404, even when the route is defined and exists

I have this route for edit and update

//Designation Details for Staffs
Route::get('designation/{staffid}', array(
  'as' => 'designation.edit',
  'uses'=>'StaffController@editDesignation'));
Route::patch('designation/update/{staffid}', array(
  'as' => 'designation.update',
  'uses'=>'StaffController@updateDesignation'));

The edit Route is working fine with the form model as:

{!! Form::model($designation, [
        'method' => 'PATCH',
        'route' => ['designation.update', $designation->staffid]
        ]) !!}

But, when the Submit button is clicked for update, it has the url

http://localhost/hrm/public/designation/update/2

and leads to 404. Just letting you know, all other routes are working fine. For similar case, another update route:

Route::patch('staff/update/{id}', array(
  'as' => 'staff.update',
  'uses'=>'StaffController@update'));

is working fine as well. TIA.

UPDATE: Controller Method updateDesignation

public function updateDesignation($staffid, Request $request){
        /*
            ** Update for Staff's Designation Information
        */
        $designation= Designation::findOrFail($staffid);

        $input = $request->all();
        //dd($input);

        $designation->fill($input)->save();    

        return view('staff.editdesignation')->with('designation',$designation)->with('staffid',$staffid);

    }

Upvotes: 0

Views: 1725

Answers (2)

Sudip Subedi
Sudip Subedi

Reputation: 81

Use can use put instead of patch. Html form doesn't support put, patch or delete options. So add a hidden _method field to the form.

Upvotes: 1

Luzan Baral
Luzan Baral

Reputation: 3698

There was a mistake on my Query

$designation= Designation::findOrFail($staffid);

$staffid was not a Primary Key but a Foreign key. I solved it by changing my query to.

$designation = Designation::where('staffid', $staffid)->firstOrFail();

Upvotes: 1

Related Questions