0

So for example if I have tables:

project

user_project

And I want to delete some project from project table and all record in user_project table related to that project, how can I do that? Can I do that in single line? Because I don't want to some project be delted but in user_project to still exists...

Vladimir Djukic
  • 2,042
  • 7
  • 29
  • 60
  • Possible duplicate of [Automatically deleting related rows in Laravel (Eloquent ORM)](http://stackoverflow.com/questions/14174070/automatically-deleting-related-rows-in-laravel-eloquent-orm) – Laerte Feb 05 '16 at 13:22

1 Answers1

0

If u set up delete cascading in your migration deleting a record in the project table will automatically delete the record from the related table if not then you have to manually delete it.

// to delete records from both table, let first delete from the 
// child table then after that we delete from the parent table.
$project = Project::find($id);

// let assume you have define the user relation in your model and 
// its a many to many association
$project->users()->detach();
$project->delete();
oseintow
  • 7,221
  • 3
  • 26
  • 31
  • I can set cascading only for InnoDB tables? with that second above way I can't 100% be sure this two part will execute together: `$project->user()->detach(); $project->delete();` , also there would help transaction but I am not sure if transaction are supported for MyISAM? – Vladimir Djukic Feb 05 '16 at 14:16
  • with the last two codes it will excecute two seperate queries. one to delete records from the user_project table and the last one from the projects table – oseintow Feb 05 '16 at 14:20
  • I know that becase of that I am not sure it will execute together always because one query can be executet but other not... Transaction can solve it but not sure if it can use with MyISAM – Vladimir Djukic Feb 05 '16 at 14:21