Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

updateOrCreate does not match Carbon date

Tags:

php

laravel

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!

like image 741
Carol.Kar Avatar asked Dec 30 '25 03:12

Carol.Kar


1 Answers

Try this when converting a string to a date with Carbon

$date = Carbon::parse($yourStringDate);
like image 170
Mohammed_40 Avatar answered Jan 01 '26 19:01

Mohammed_40



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!