Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

GregorSams's avatar

updateOrCreate does not match Carbon date

I am using Laravel Framework 6.16.0 and I am creating from a string a date object so that I can input it in my db:

                        $transaction_date = Carbon::createFromFormat('Y-m-d', $tradeDate);
                        $filling_Date = Carbon::createFromFormat('Y-m-d H:i:s', $fillingDate, 'UTC');

                        $product = Product::updateOrCreate(
                            [
                                'price' => trim($price),
                                'qty' => $qty,
                                'companies_id' => $company->id,
                                'persons_id' => $person->id,
                                'amount_range' => $owned,
                                'filling_date' => $filling_Date,
                                'transaction_date' => $transaction_date,
							],
                            []
                        );

When running the above query my product does not get found as $filling_Date and $transaction_date are not matched in the database, even if my product already exists.

I am guessing, the reason is that I am creating a "new" Carbon object.

Any suggestions how to match the filling_date and transaction_date in the database?

I appreciate your replies!

0 likes
2 replies
GeordieJackson's avatar

You can use 2 arrays with the updateOrCreate() method. The first array holds the keys you want to match by and the second the ones you want to update/insert.

e.g.

$product = Product::updateOrCreate(
    ['product_id' => $product_id],
    [
        'product_id' => $product_id,
        'price' => trim($price),
        'qty' => $qty,
        'companies_id' => $company->id,
        'persons_id' => $person->id,
        'amount_range' => $owned,
        'filling_date' => $filling_Date,
        'transaction_date' => $transaction_date,
     ]);

I've added the field 'product_id' to illustrate how to do it with a unique field.

Matching the record this way overcomes the problems associated with date fields - especially the 'updated_at' field when using timestamps.

SilenceBringer's avatar

Hi @gregorsams

What type of columns you have for transaction_date and filling_date in database? Are they date or datetime?

If transaction_date is date and filling_date is datetime (as I can suggest from the formats), and because they have standard format, you can skip carbon instance creation and use it directly. So, try something like:

// comment it out
// $transaction_date = Carbon::createFromFormat('Y-m-d', $tradeDate);
// $filling_Date = Carbon::createFromFormat('Y-m-d H:i:s', $fillingDate, 'UTC');

$product = Product::updateOrCreate(
    [ // here is the data we search for. In this particular case - filling date and transaction date
        'filling_date' => $fillingDate,
        'transaction_date' => $tradeDate,
    ],
    [ // here is the data we wat to update if record found
        'price' => trim($price),
        'qty' => $qty,
        'companies_id' => $company->id,
        'persons_id' => $person->id,
        'amount_range' => $owned,
    ]
);
// if record not found, laravel will merge those 2 arrays and create new product with the merged data

Please or to participate in this conversation.