Friday, 15 June 2012

laravel - Soft deleting with a relationship -



laravel - Soft deleting with a relationship -

i have book , author table. author has many books. book has 1 author.

i wish soft delete author.

so have deleted_at field in authors table (do need on books table?). there author_id foreign key in books table.

i call:

author::destroy($id);

but error:

cannot delete or update parent row: foreign key constraint fails

i'm using larval 4.1

here migrations:

schema::create('authors', function($table) { $table->increments('id'); $table->string('name'); $table->timestamps(); $table->softdeletes(); }); schema::create('books', function($table) { $table->increments('id'); $table->integer('author_id')->unsigned(); $table->foreign('author_id')->references('id')->on('authors'); $table->string('title'); // varchar $table->string('isbn'); // varchar });

ever thought of cascaded deletes ?

//suggested implementation , work ? $table->foreign('author_id')->references('id')->on('authors')->ondelete('cascade'); // current implementation $table->foreign('product_id')->references('id')->on('products')->ondelete('cascade');

laravel laravel-4 eloquent

No comments:

Post a Comment