本文介绍了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).
    • 示例:

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

      • delete 只能在查询构建器中调用
        • delete can only be called in query builder
        • 示例:

          $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

          这篇关于Laravel 中的 destroy() 和 delete() 方法有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 10:07