简体   繁体   中英

Laravel book on delete remove related all posts and comments

this snippets is from my Book model:

 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();
     }
 }

in Post model I wrote:

 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();
     }

in Comment model I wrote:

 function posts() {

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

     function user() {

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

Now, when I try to remove the book it says. Call to undefined method Illuminate\\Database\\Query\\Builder::comments()

在此处输入图片说明

So, I understand the book models delete comments isn't working right, but don't understand how to resolve it.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM