dmgd
dmgd

Reputation: 445

laravel using foreign keys

I just cant see this I have events, users, items tables

events
_______________________
id | name

users
______________________
id | name

items
_______________________
id | event_id  | user_id. | name

I want items deleted when either an event or user are deleted

Schema::table('items', function(Blueprint $table) {
        $table->foreign('user_id')->references('id')->on('users');
    });
Schema::table('items', function(Blueprint $table) {
        $table->foreign('events_id')->references('id')->on('events');
    });

So if I add ->onDelete('cascade') to both when the user or event are deleted the item will be also deleted?

Upvotes: 0

Views: 38

Answers (1)

Paul Spiegel
Paul Spiegel

Reputation: 31792

So if I add ->onDelete('cascade') to both when the user or event are deleted the item will be also deleted?

That's correct.

When you delete a row from the users table, all rows in the items table with the same user_id will be deleted.

When you delete a row from the events table, all rows in the items table with the same event_id will be deleted.

Upvotes: 2

Related Questions