TheRealPapa
TheRealPapa

Reputation: 4539

Laravel 5.8 access new row ID during creating method

My User Class uses a Trait which creates tables on behalf of a new user registered.

I want to access the new user id number to use it in the table naming like below (which does not work).

static::creating(function($obj)
{
    if (empty($obj->db_name)) {
        $obj->attributes['table_id'] = 'user_' . $obj->id;
    }
});

How can I access the id of this new user during the creating process so I can create and save the record without going back using created method to update the column.

Upvotes: 0

Views: 26

Answers (1)

Dwight
Dwight

Reputation: 12450

creating occurs before the record is saved - it has no ID yet. You may want to hook into the created event instead, which occurs after the record has been saved and has an ID.

Upvotes: 1

Related Questions