1

I am working on a system and it's working perfectly. Now, i need to create a custom update method which will only update selected columns. The main update works fine but we only need to update a few columns, not every field. So i added two new functions on my EmployeeController on top of the basic index, create, update, store, destroy and delete.

public function editphoto($EmployeeID)
{
    $employee=Employee::find($EmployeeID);

    return view('employees.editphoto',compact('employee'));      
}

public function updatephoto($EmployeeID)
{
    return view('hello');   
}

On my routes.php file, i added two new routes

Route::resource('employees', 'EmployeesController');
Route::get('employees/{employee}/editphoto', 'EmployeesController@editphoto')->name('employees.editphoto');
Route::get('employees/{employee}', 'EmployeesController@updatephoto')->name('employees.updatephoto');

On my new editphoto.blade.php view

{!! Form::model($employee,['method' => 'PUT','route'=>['employees.updatephoto',$employee->EmployeeID]]) !!}

{!! Form::label('GrandFathersName', 'Grand Fathers Name') !!}
{!! Form::text('GrandFathersName',null,['class'=>'form-control']) !!}

<a class="btn btn-success pull-left form-control" href="{{ URL::route('employees.index') }}">Cancel</a>

{!! Form::close() !!}

When i click the update button on this form, it tries to validate the data, which is actually on the update function of the controller. But i should have gotten a view with a text 'hello'

I thought it was the PATCH method that was causing it to go to the update method, so i tried to change it and even remove it, but it either throws an error or the same thing.

Here is the route list.

route list

I've tried the solution on Add new methods to a resource controller in Laravel even though it is for laravel 4. I didn't try the second answer, although it was not marked as a solution. Besides, You can see that i have added the proper routes on the Controller.

So, how can i create a new update method with a PATCH action request or how can i update the data with a new method with a PUT or any other action request?

Community
  • 1
  • 1
EtDes
  • 85
  • 9
  • Your route named `employees.updatephoto` is defined to accept `GET` requests, yet your form method with the action to that route is `PUT`. Change the route definition to match. – Bogdan Nov 12 '15 at 20:03
  • @JosephSilber Actually, i saw that answer but the selected answer doesn't apply since i've registered my new route on the routes.php file – EtDes Nov 12 '15 at 20:20
  • @Bogdan, that was my point actually, how can the employees.updatephoto route accept PATCH requests. I've already tried with PUT on the form action, but it tries to validate the data, which means, it's going into either the store or update function. – EtDes Nov 12 '15 at 20:27

1 Answers1

1

Your problem is that the employees.update route already defined by the Route::resource matches the incoming URL path and HTTP verb when you try to update the photo.

There is no difference between the path employees/{employees} defined by the resource and employees/{employee} defined by you, because the path variable name doesn't matter when matching, so it will always match the route registered first. The solution is easy in this case, just use a different path definition for updating the photo, for example:

Route::put('employees/{employee}/updatephoto', 'EmployeesController@updatephoto')->name('employees.updatephoto');

With this change alone your edit photo form should now work.

Bogdan
  • 43,166
  • 12
  • 128
  • 129
  • Yep, exactly what i needed! Thanks :) – EtDes Nov 12 '15 at 20:54
  • By the way, is there a way to directly access the updatephoto function without first going to the editphoto page. I tried to add a button which directly links to the updatephoto function but i get MethodNotAllowedHttpException error. – EtDes Nov 13 '15 at 20:23