Laravel manages records using RDB (multiple tables).
When trying to delete data, the following error occurred:
] (2b8914bd604a871f9293077a73eb6c2c.png)p>

Destroy action code

public function destroy ($id)
        Category :: find (Drill :: find ($id)->category_id)->delete ();
        Problem :: where ('drill_id', $id)->first ()->delete ();
        Drill :: find ($id)->delete ();
        return redirect ('/ drills')->with (' flash_message ',! [Image description] (0f96e3165ff1a0480c8fee557edf7b51.png) leted'));

・ Table structure (Relations are established according to the line in the figure)

If there is a foreign key constraint, it cannot be deleted because there is a column linked to the parent table (to maintain consistency between tables).

It doesn't mean that there must be external constraints, but in Ruby the same case It seems that you can execute DELETE by setting options .

Laravel may have such options? I looked through the reference, but I didn't see that in the hasMany/destroy/DELETE page.

How can I delete a record that spans multiple tables with relations in place?
Please give me advice.

  • Answer # 1

    Is it a question that can't be the same ason delete cascadewhen

    create is executed?
    If you are making a table with a migration file, you can look around the word ondelete in ↑, but this is an old document, so look at the one that matches your version of laravel

    I don't personally like on delete cascade, so don't use it