Laravel: How to delete rows from multiple table wi

2019-02-25 00:25发布

I have this code to delete data from multiple tables in one go:

DB::table('tb_stikes_register_school')->where('register_id', $_POST['id'])->delete();
            DB::table('tb_stikes_register_guardian')->where('register_id', $_POST['id'])->delete();
            DB::table('tb_stikes_register_student')->where('register_id', $_POST['id'])->delete();

I'm trying to shorten this into 1 query only, register_id from guardian and school tables is the foreign key of student table. I've been trying to use join but only student table record is deleted. Is there any workaround this?

1条回答
闹够了就滚
2楼-- · 2019-02-25 00:58

Something like this maybe - haven't tested

DB::table(DB::raw('FROM tb_stikes_register_school, tb_stikes_register_guardian, tb_stikes_register_student'))
->join(ENTER JOIN INFO) // wasn't clear how your tables were related
->where('register_id', $_POST['id'])
->delete();

Or you could use a fully raw query:

 DB::query('SQL statement here');

Basically recreating something similar to this: delete rows from multiple tables

查看更多
登录 后发表回答