php 如何在 Laravel 5 中删除单个记录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33688066/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to delete a single record in Laravel 5?
提问by Si Va
Using Laravel 5 I m trying to delete a single record within a controller, here is my code:
使用 Laravel 5 我试图删除控制器中的单个记录,这是我的代码:
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 ?
我如何修复实现?
回答by Luis González
From the official Laravel 5 documentation:
来自官方 Laravel 5 文档:
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:
在每种情况下,括号之间的数字代表对象 ID,但您也可以对一组模型运行删除查询:
$affectedRows = User::where('votes', '>', 100)->delete();
回答by Saad
So the Laravel's way of deleting using the destroy
function is
所以Laravel使用destroy
函数删除的方式是
<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-buttonAnd your route should look something like this
你可以在这里找到一个例子http://laravel.com/docs/5.1/quickstart-intermediate#adding-the-delete-button你的路线应该是这样的
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.
它适用于例如:Route::resource('employee', 'EmployeeController');
并且它还应该适用于您设置销毁路线的方式。
回答by Amarnasan
Obviously you have a bad routing problem. You're trying to use a 'get' verb to reach a route defined with a 'delete' verb.
显然,您的路由问题很严重。您正在尝试使用“get”动词到达用“delete”动词定义的路线。
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.
或者继续使用删除动词,但随后您需要使用一个形式(而不是锚点),其中的参数称为_method
和值,'delete'
说明您正在使用“删除”动词。
回答by loqman chookili
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>