Back

Eloquent sync - Which Event is fired?


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?

TerrePorter replied 2 years ago

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.

brnlbs replied 2 years ago

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.

fico7489 replied 1 week ago

try with this package https://github.com/fico7489/laravel-pivot


Sign in to participate in this thread!



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