Using Laravel 5 I m trying to delete a single record within a controller, here is my code:
public function destroy($id)
{
$employee = Employee::find($id);
$employee->delete();
return Redirect::route('noorsi.employee.index');
}
My view page code is:
<td><a href="employee/{{$employee->id}}/destroy" class="btn btn-default">Delete</a></td>
My route is:
Route::delete(employee.'/{id}', array('as' => 'noorsi.employee.destroy','uses' => Employeecontroller.'@destroy'));
That did not work.
How do I fix the implementation ?
From the official Laravel 5 documentation:
Delete an existing Model
Deleting An Existing Model By Key
In every cases, the number between brackets represents the object ID, but you may also run a delete query on a set of models:
http://laravel.com/docs/5.0/eloquent#insert-update-delete
So the Laravel's way of deleting using the
destroy
function isYou can find an example here http://laravel.com/docs/5.1/quickstart-intermediate#adding-the-delete-button And your route should look something like this
It works with eg:
Route::resource('employee', 'EmployeeController');
and it should also work with how you set up your destroy route.Obviously you have a bad routing problem. You're trying to use a 'get' verb to reach a route defined with a 'delete' verb.
If you want to use an anchor to delete a record, you should add this route:
or keep using a delete verb, but then you need to use a form (not an anchor) with a parameter called
_method
and value'delete'
stating that you're using a 'delete' verb.