jeudi 18 janvier 2018

How to delete multi relationship Model in Laravel 5.5

I have 3 tables

books: - id - name - deleted_at

posts: - id - book_id - connent - deleted_at

comments: - id - post_id - comment - deleted_at

A book has many posts, a post has many comments.

I want to delete posts and comments relate to a book when I delete the book. I already add relationship in all Models, all table use SoftDelete, and try to use Event in Laravel

// Book Model
protected static function boot() {
    parent::boot();
    static::deleting(function($book) {
         $book->posts()->delete();
    });
}

// Post Model
protected static function boot() {
    parent::boot();
    static::deleting(function($post) {
         $book->comments()->delete();
    });
}

When I try to delete a book:

$book = Book:find($bookId);
$book->delete()

Book and Posts, deleted, but Comments are not deleted. Could I delete comments with Laravel Event? Thank for readding!



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2mN64xs
via IFTTT

Aucun commentaire:

Enregistrer un commentaire