How to delete a single record in Laravel 5?
Solution 1
From the official Laravel 5 documentation:
Delete an existing Model
$user = User::find(1);
$user->delete();
Deleting An Existing Model By Key
User::destroy(1);
User::destroy([1, 2, 3]);
User::destroy(1, 2, 3);
In every cases, the number between brackets represents the object ID, but you may also run a delete query on a set of models:
$affectedRows = User::where('votes', '>', 100)->delete();
http://laravel.com/docs/5.0/eloquent#insert-update-delete
Solution 2
So the Laravel's way of deleting using the destroy
function is
<form action="{{ url('employee' , $employee->id ) }}" method="POST">
{{ csrf_field() }}
{{ method_field('DELETE') }}
<button>Delete Employee</button>
</form>
You 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
Route::delete('employee/{id}', array('as' => 'employee.destroy','uses' => 'Employeecontroller@destroy'));
It works with eg:Route::resource('employee', 'EmployeeController');
and it should also work with how you set up your destroy route.
Solution 3
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:
Route::get('/employee/{id}/destroy', ['uses' => 'EmployeeController@destroy']);
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.
Solution 4
Route::get('/showcon/{del_id}/delete','MainController@deletemsg');
public function deletemsg($del_id){
$mail=Mail::find($del_id);
$mail->delete($mail->id);
return redirect()->back();
}
<a href="showcon/{{$m->id}}/delete">del</a>
Related videos on Youtube
Si Va
Updated on November 03, 2020Comments
-
Si Va over 3 years
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 ?
-
Saad over 8 yearsDoes it give any errors?
-
Si Va over 8 yearsIt redirects to the page: serviceweb:8000/employee/07716a5ed38e4e168e2178a9de201fb7/… and stats that 'Whoops, page not found.'
-
Amarnasan over 8 yearsWhat is your
routes
line? I'd bet the 'destroy' part should be before the 'id' part -
Si Va over 8 yearsRoute::delete($key.'/{id}', array('as' => 'noorsi.'.$key.'.destroy','uses' => $controller.'@destroy'));
-
Andrius over 8 yearsChange the Route method to
Route::get()
since now you're doing a GET method by opening the link, but you tell Laravel to expect a DELETE method to actually call the controller's method. You're not even calling the function, currently. -
ricktap over 8 yearsYou are calling the destroy method of your controller per GET request, please update your question with the route file. You probably have to call the method with a delete request, or it'll just return a 404
-
-
ricktap over 8 yearsEven though this would work, it would be better to change the view code, to send a delete method. This is the default behavior in Laravel and is well documented, so that coming back to his code, would make it easy to understand what he's doing. So the author should probably go with your second suggestion
-
Amarnasan over 8 years@ricktap Yes. Like 90% of the times, there is a "fast" solution, and a "right" solution.
-
Fefar Ravi almost 2 years+1, Also read: How To Delete Record By ID using Laravel Eloquent