Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Model::create -> ID is always 0 #59

Open
monkeymonk opened this issue Aug 2, 2019 · 2 comments
Open

Model::create -> ID is always 0 #59

monkeymonk opened this issue Aug 2, 2019 · 2 comments

Comments

@monkeymonk
Copy link
Contributor

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 monkeymonk changed the title Model create ID always 0 Model::create -> ID always 0 Aug 2, 2019
@monkeymonk monkeymonk changed the title Model::create -> ID always 0 Model::create -> ID is always 0 Aug 2, 2019
@monkeymonk
Copy link
Contributor Author

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.

@ijakparov
Copy link

ijakparov commented Aug 22, 2019

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 #31 (comment)

And if u fix create u also will fix firstOrCreate etc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants