欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

在Laravel destroy()和delete()方法之间的区别是什么?What is the difference between destroy() and delete() methods in Laravel? 有大用


I'm having a minor issue with Laravel 4. I'd like to use thedelete()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 passTeetime::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);
        }
 

1 Answer  正确答案

  • destroyis correct method for removing an entity directly (via object or model).

Example:

$teetime = Teetime::where('date', '=', $formattedDate)->firstOrFail();
$teetime->destroy();
  • deletecan 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

 
3 
Thanks but why does the documentation also provide this example:$user = User::find(1); $user->delete();? I've useddelete()elsewhere in my code and it has been working fine.user347284Mar 25 '14 at 8:33
  
I have updated my answer, please check it :) It explains it in detail.phoopsMar 25 '14 at 8:34
  
Oh, I get it. I think it'sfirstOrFail()that gets me out of Query Builder.user347284Mar 25 '14 at 8:36
  
Yes, as it returns an object (model).phoopsMar 25 '14 at 8:36
  
I'll accept your answer as soon as I can. Thanks!user347284Mar 25 '14 at 8:36

来自 https://stackoverflow.com/questions/22628981/what-is-the-difference-between-destroy-and-delete-metho...

我有一个小问题与laravel 4。我想用delete()方法在记录但由于某种原因,它实际上并没有删除记录。destroy()的确,虽然,所以我的代码是好的。还有,如果我通过Teetime::where('date', '=', $formattedDate)->count()我认为我是正确的。你有什么不舒服吗?

        if($action=="delete") {
            $teetime = Teetime::where('date', '=', $formattedDate)->firstOrFail();
            // for some reason $teetime->delete() doesn't work
            Teetime::destroy($teetime->id);
        }
 

1回答 正确答案

 
了投票四十四投票表决认可的
  • destroy是删除一个实体直接修正法(通过对象或模型)。

例子:

$teetime = Teetime::where('date', '=', $formattedDate)->firstOrFail();
$teetime->destroy();
  • delete只能在查询生成器称为

例子:

$teetime = Teetime::where('date', '=', $formattedDate)->delete();

从文件:

删除现有模型的关键

User::destroy(1);

User::destroy(array(1, 2, 3));

User::destroy(1, 2, 3);

当然,你也可以运行一个删除一组模型的查询:

$affectedRows = User::where('votes', '>', 100)->delete();

更多信息网址:/ / /文档/ eloquent laravel.com

分享提高这个答案

来自 https://stackoverflow.com/questions/22628981/what-is-the-difference-between-destroy-and-delete-metho...

普通分类: