Awesome q2a theme

As the hasMany relationship should be deleted?

0 like 0 dislike
A bit I do not understand how to remove the hasMany relationship. I have a branch table and room. That is, one branch has several rooms. I tried in the documentation to find but so far nothing found, if only foreach to sort through everything and delete records
That's what the error is out
"SQLSTATE[23503]: Foreign key violation: 7 ERROR: UPDATE or DELETE in the table "branch" violates foreign key constraint "room_branch_id_foreign" table "room"
DETAIL: key (id)=(4) still have the links in the table "room". (SQL: delete from "branch" where "id" = 4)"

In the controller
public function destroy($id) { return Branch::destroy($id); }

In the photo above
class Branch extends Model { protected $table = 'branch'; protected $dateFormat = 'U'; protected $fillable = ['name', 'city', 'street', 'build', 'b', 'phone', 'user_id']; public function rooms() { return $this->hasMany('Growth\oom'); } }

There is certainly such an example to do
public function destroy($id) { $branch = Branch::find($id)->rooms(); $branch->delete(); return Branch::destroy($id); }

Or I misunderstand to delete related tables?
by | 78 views

1 Answer

0 like 0 dislike
You have sent a request for deletion, but deletion is not possible because in the database a record that references this foreign key.

You must either first remove from the database records referring to deleted or to identify in the database the behavior when you delete such a record. For example, if on delete cascade records referencing the deleted will be automatically deleted the database itself.

Google mysql on delete

Related questions

0 like 0 dislike
2 answers
0 like 0 dislike
4 answers
0 like 0 dislike
2 answers
asked Mar 25, 2019 by sdevalex
0 like 0 dislike
1 answer
asked May 11, 2019 by LysenkoSasha
0 like 0 dislike
1 answer
110,608 questions
257,187 answers
40,796 users