Support the ongoing development of Laravel.io →
Database Eloquent Packages

I am currently building an application and I am using UUIDs instead of auto-incrementing IDs. To simplify the UUID stuff I use the https://github.com/kirkbushell/eloquence package.

The package fires the UUID generation whenever Eloquent fires the creating event. This all works fine except when I use attach or sync. When I use either of these two I get an exception that the ID is null and when I dd() the Model::creating it doesn't die so the event didn't get fired, it also doesn't die at saving or updating.

SQLSTATE[23502]: Not null violation: 7 ERROR: null value in column "id" violates not-null constraint

So my question is to which Eloquent event I would need to listen to do something on attach/sync or what else would I need to do?

Last updated 2 years ago.
0

I found this when I was researching events a bit ago,

use Illuminate\Database\Eloquent\Relations\Pivot;

Pivot::creating(function($pivot) {
    $pivot->id = (string)$this->generateNewId();
});

From https://laracasts.com/discuss/channels/general-discussion/eloquent-attach-which-event-is-fired

Hope that helps.

0

Yeah, I posted that some time ago but the Pivot events also don't get fired when I use sync. On attach it worked on the problem I had in the thread but it doesn't work for sync.

0

Sign in to participate in this thread!

Eventy

Your banner here too?

brnlbs brnlbs Joined 27 Aug 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.