'Can't get ID from created Eloquent model extending Pivot in Laravel

Maybe simple, but I can't figure it out...

When I create a record using Eloquent and a model that extends Model, and then get its id right after it just works:

$example = Example::create(['name'=> 'exie']);
dd($example->id);
// returns id (ex. 15) as expected from the created record...

When I create a record using a model that extends Pivot and try to get id, it only returns null.

$customPivotExample = CustomPivot::create(['name' => 'custie']);
dd($customPivotExample->id);
// returns null instead of id...

The records all have a PK so I expected to just get the ID back, but apparently there is something about using a custom pivot model and getting it's id after creation what I am overlooking..

(examples are really simple but the actual code only contains more key=>value pairs and nothing more)

anyone has any idea?



Solution 1:[1]

Own Answer

Putting this here because this is not written (somewhat) in the Laravel documentation.

They mention this about auto incrementing ID's: https://laravel.com/docs/9.x/eloquent-relationships#custom-pivot-models-and-incrementing-ids

I had not done this (my bad), but doing this also enables getting the ID after creation of a pivot record as in my second example....

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ouflak