Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kfirba's avatar
Level 50

attach model to m-m relationship if not exist

Hello.

I have User and Coupon and I want that whenever a user views a coupon, I will insert the data into a coupon_user table if the user hasn't already done viewed this coupon before - insert if not exist.

How would you go about doing this with the least queries possible?

0 likes
5 replies
mrberggg's avatar

You could either get the current ids, then sync or you could run a query to see if the relationship currently exists and only add the relationship if it currently doesn't exist. If you're only saving a single coupon I'd choose the latter approach.

kfirba's avatar
Level 50

@mrberggg So basically you are saying that I should do something like:

$coupon = $user->coupons()->where('coupon_id', $coupon_id)->first();

if (is_null($coupon) {
    $user->coupons()->attach($coupon_id);
}
2 likes
rodrigo.pedra's avatar

->sync(...) accepts a second parameter that tells it not to detach related models not being synced. If you look in the source code it will do basically the same thing then you described (list current, diff, attach new)

For your code this should do it:

// note the false boolean as the second parameter, it will skip detaching (e.g. deleting)
// ids not being synced
$user->coupons()->sync($coupon_id, false); 
7 likes
kordy's avatar

an alias to sync($ids, false) :

$user->coupons()->syncWithoutDetaching($ids)
6 likes

Please or to participate in this conversation.