Laravel关于删除的书删除相关的所有帖子和评论

时间:2014-06-12 14:09:35

标签: php laravel laravel-4 orm

此片段来自我的Book模型:

 class Book extends \Eloquent
 {  

 public function delete()

     {
         // delete all related cover photos
         $this->cover()->delete();
         // delete all related comments photos
         $this->posts()->comments()->delete();
         // delete all related Blurbs
         $this->posts()->delete();

         // delete the model
         return parent::delete();
     }
 }

Post模型中我写道:

 function book() {

         return $this->belongsTo('Book');

     }

     function comments() {

         return $this->hasMany('Comment');

     }

     function user() {

         return $this->belongsTo('User');

     }

 public function delete()
     {

         // delete all related comments also
         $this->comments()->delete();
         // delete the model
         return parent::delete();
     }

Comment模型中我写道:

 function posts() {

         return $this->belongsTo('Post');
     }

     function user() {

         return $this->belongsTo('User');
     }

现在,当我试图删除这本书时,它说。 Call to undefined method Illuminate\Database\Query\Builder::comments()

enter image description here

所以,我理解书模型删除评论不正常,但不明白如何解决。

1 个答案:

答案 0 :(得分:1)

您可能需要使用正确的索引,外键更新数据库架构,并且正如@Razor所说,级联...请参阅http://laravel.com/docs/schema#foreign-keys