本文介绍了Laravel中的destroy()和delete()方法有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在Laravel 4中遇到一个小问题。我想在记录上使用 delete()
方法,但由于某种原因,它并不实际删除记录。 destroy()
尽管如此,我的代码是好的。此外,如果我通过 Teetime :: where('date','=',$ formattedDate) - > count()
给我的看法,我得到一个是正确的。有什么问题? if($ action ==delete){
$ teetime = Teetime :: where ('date','=',$ formattedDate) - > firstOrFail();
//由于某种原因$ teetime-> delete()不起作用
Teetime :: destroy($ teetime-> id);
}
解决方案
-
destroy
是直接(通过对象或模型)删除实体的正确方法。
示例:
$ teetime = Teetime :: where('date','=',$ formattedDate) - > ; firstOrFail();
$ teetime-> destroy();
-
删除
只能在查询构建器中调用
示例:
$ teetime = Teetime :: where('date','=',$ formattedDate) - > delete();
从文档中:
删除现有模型按键
User :: destroy(1);
User :: destroy(array(1,2,3));
用户:: destroy(1,2,3);
当然,您也可以在一组模型上运行删除查询:
$ affectedRows = User :: where('votes','>',100) - > delete()
更多信息:
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
is correct method for removing an entity directly (via object or model).
Example:
$teetime = Teetime::where('date', '=', $formattedDate)->firstOrFail();
$teetime->destroy();
delete
can only be called in query builder
Example:
$teetime = Teetime::where('date', '=', $formattedDate)->delete();
From documentation:
Deleting An Existing Model By Key
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();
More info: http://laravel.com/docs/eloquent
这篇关于Laravel中的destroy()和delete()方法有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!