wp-eloquent icon indicating copy to clipboard operation
wp-eloquent copied to clipboard

Model::create -> ID is always 0

Open monkeymonk opened this issue 5 years ago • 2 comments

When creating a new object, the id is always 0.

$user = CustomUser::create([
    'email' => '[email protected]',
]);
// => ['id' => 0, 'email' => '[email protected]']

Using solution found in issues/12. I've noticed that it throw then a duplicate ID error.

The id field is in auto increment in DB config.

Any clues ?

monkeymonk avatar Aug 02 '19 08:08 monkeymonk

For now, I solved using :

public static create(array $attributes = [])
{
    if (!isset($attributes['id'])) {
        $attributes['id'] = self::all()->last()->id + 1;
    }

    return parent::create($attributes);
}

Crappy... but working.

monkeymonk avatar Aug 02 '19 09:08 monkeymonk

For now, I solved using :

public static create(array $attributes = [])
{
    if (!isset($attributes['id'])) {
        $attributes['id'] = self::all()->last()->id + 1;
    }

    return parent::create($attributes);
}

Crappy... but working.

I've the same bug. Coused by wrong table prefix. Use protected $table with prefix and remove all logi from getTable method (it duplicates prefix)

Also put save method to your model to see errors https://github.com/tareq1988/wp-eloquent/issues/31#issuecomment-386458757

And if u fix create u also will fix firstOrCreate etc

ijakparov avatar Aug 22 '19 09:08 ijakparov