wp-eloquent
wp-eloquent copied to clipboard
Model::create -> ID is always 0
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 ?
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.
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