Laravel 中的 destroy() 和 delete() 方法有什么区别?
我在使用 Laravel 4 时遇到了一个小问题.我想对记录使用 delete()
方法,但由于某种原因它实际上并没有删除记录.destroy()
确实如此,所以我的代码很好.另外,如果我将 Teetime::where('date', '=', $formattedDate)->count()
传递给我的视图,我会得到一个正确的.有什么问题?
I'm having a minor issue with Laravel 4. I'd like to use the delete()
method on a record but for some reason it doesn't actually delete the record. destroy()
does, though, so my code is good. Also, if I pass Teetime::where('date', '=', $formattedDate)->count()
to my view I get one which is correct. What's the problem?
if($action=="delete") {
$teetime = Teetime::where('date', '=', $formattedDate)->firstOrFail();
// for some reason $teetime->delete() doesn't work
Teetime::destroy($teetime->id);
}
推荐答案
destroy
是直接(通过对象或模型)删除实体的正确方法.destroy
is correct method for removing an entity directly (via object or model).delete
只能在查询构建器中调用delete
can only be called in query builder
示例:
$teetime = Teetime::where('date', '=', $formattedDate)->firstOrFail();
$teetime->destroy();
示例:
$teetime = Teetime::where('date', '=', $formattedDate)->delete();
来自文档:
User::destroy(1);
User::destroy(array(1, 2, 3));
User::destroy(1, 2, 3);
当然,您也可以对一组模型运行删除查询:
Of course, you may also run a delete query on a set of models:
$affectedRows = User::where('votes', '>', 100)->delete();
更多信息:http://laravel.com/docs/eloquent
相关文章