#firstOrCreate Issue

14 messages · Page 1 of 1 (latest)

drifting latch
#

I'm sure I'm doing something stupid here, but I just can't figure it out at all.

    ['tld' => $domain_tld],
    ['cost' => 0, 'test' => 'boo']
);```

My reading of the docs suggests that the above should;
a) Find and return a record that matches 'tld',
b) If it can't find a matching record, it will create one using a merger of both arrays,
c) There should be no updating at all.

```The firstOrCreate method will attempt to locate a database record using the given column / value pairs. If the model can not be found in the database, a record will be inserted with the attributes resulting from merging the first array argument with the optional second array argument:```

What's actually happening is that it's finding a record using 'tld' and it's then _updating_ that found record with the values from the second array.

Before & after record screenshots attached for reference.

Am I miss-reading the docs?
Have I made a stupid mistake somewhere?
Am I just not seeing the wood for the trees?

Thanks!
nimble bobcat
#

Seems to me the code that is causing this error might be elsewhere.

Your firstOrCreate would find a matching Tld or create one with the cost = 0 and test = 'boo'
But if I understood that correctly, your 'after' screenshot shows one result with these columns as NULL, not 0 and 'boo' so it can't be the firstOrCreate execution.

What I believe is happening here is that somewhere else in the code, these columns are being set as null.

Showing more code might help if it wouldn't be a problem to you

drifting latch
#

Just to clarify the "before" screenshot is the one with the prices (and no timestamps). The "after" is the one filled with 0 cost and 'boo' test.

#

So that firstOrCreate is updating the existing records, replacing the price values and inserting timestamps and 'boo'.

#

For some reason Discord swapped the order, apologies.

#

I deliberately added the 'boo' part so I could be sure that it was that firstOrCreate that was modiying the records.

#

There's no other code at all touching anything to do with Tld.

nimble bobcat
#

Hmm, that is weird. By definition the firstOrCreate method shouldn't update anything:

public function firstOrCreate(array $attributes = [], array $values = [])
    {
        if (! is_null($instance = (clone $this)->where($attributes)->first())) {
            return $instance;
        }

        return $this->createOrFirst($attributes, $values);
    }

What laravel version are you using? Is there a chance someone else altered anything in the core files? This all seems very weird to me, never encoutered a bug like this

drifting latch
#

I'm glad it's not just me! 😂

#

No, this is a fresh (local) install, no one else involved but me.

#

Latest 11

#

I'm at a loss.

#

I'll split it, I'll just do a find and then a conditional create, it's messy but this is only a proof of concept for now.

nimble bobcat
#

I would also try creating a new fresh install and try the same code