php - Laravel how to delete a row from multiple tables in one query -
in question answers project, admin has functionality view questions , delete each 1 if wishes. 1 question id has 5 related tables. , each question id present in 5 tables in db , when admin deletes 1 question have delete entries these 5 tables based on question id.
in controller
db::table('user_questions')->where('question_id','=',$question_id)->delete(); db::table('masters_answers')->where('question_id','=',$question_id)->delete(); db::table('masters_questions')->where('question_id','=',$question_id)->delete(); db::table('question_to_category')->where('question_id','=',$question_id)->delete(); db::table('question_to_tags')->where('question_id','=',$question_id)->delete(); return redirect::action('admincontroller@anyshowquestions', array());
the above code works there way same procedure in 1 db query in laravel.? had referred post cannot find solution mine. helpful if suggest right way??
you can set foreign keys on related tables. add ondelete('cascade')
when creating keys in migrations , if ok, then, when delete question automatically delete related items too.
or
use db::statement
expression respective sql query.
example:
db::statement('update foo set bar=2');
you need solve task in plain sql, , put resulting query in db::statement
, viola.
to query, can read article or stackoverflow post
Comments
Post a Comment