🤹
TALL Stack Tips
  • What is TALL Stack
  • Tailwind
    • Tailwind Resources
    • Swinging Bell Notification Icon
    • Styled Unordered Lists
  • Alpine
  • Alpine Resources
  • Tabbed Content Using Alpine JS
  • Checkbox component with SVG tick
  • Dropdown animation
  • Create a Sliding Puzzle Captcha
  • Laravel
    • Tabler Icons Component
    • Password-less Login with Laravel 8+
    • Password-less Login with Magic Link in Laravel 8
    • Laravel Resources
    • Laravel Breeze Login Conditional Redirect
    • Jetstream Login Conditional Redirect
    • Simplify Laravel CRUD Controllers
    • CSRF and expired login forms
    • CSRF and expired logout forms
    • Add your own logo to Laravel Mail
    • Specify a different mail theme for Notifications
    • Show custom page when email verification link expired
    • Using a mutator to save currency
    • Using Spatie Valuestore to hold frequently accessed settings
    • Using the old() helper
    • Alternatives to using Eloquent Accessor
    • UpdateOrCreate may not update timestamp
    • Use of lockForUpdate()
    • Using S3
    • Super Simple User Based Feature Flags
    • Installing a Specific Version of Laravel
    • Versioning your Laravel Project
    • CSS Cache Busting with your Git Commit SHA
    • Adding column to Database Notifications table
    • Find nearby locations using the Haversine formula in Eloquent query
    • Using Queues on Shared Hosting with Laravel
    • Create Guaranteed Unique Invoice Number in Laravel
    • Send Notification to all team members
    • Protect Staging site with Basic Auth
    • Working with Enums
    • PHP DateTime formatting cribsheet
  • Livewire
    • Livewire Resources
    • Naming Livewire Components
    • Dynamic Cascading Dropdown with Livewire
    • Hiding a button after click
    • Working with Javascript Components
    • SweetAlert2 with Livewire
    • Select Multiple or Checkboxes
    • Clearing checkboxes in Livewire
    • Livewire File Uploads Using S3
    • Simple Log File Viewer
  • Related Resources
    • Testing resources
    • When Composer runs out of memory
    • Deployment
    • Security
    • Scheduler & Cron tips
    • LastPass tips
    • Using Git
    • VSCode Tips
    • Markdown
    • Cpanel resources
Powered by GitBook
On this page

Was this helpful?

  1. Laravel

UpdateOrCreate may not update timestamp

When the record is identical, the updated_at timestamp is not changed

Consider the following code which is part of a spreadsheet import of a product catalogue.

    Item::updateOrCreate(
        ['code' => $row[0]],
        [
            'description'=> $row[1],
            'category'=> $row[2],
            'uom' => $row[3],
            'case_quantity' => $row[4],
            'each' => intval(strval($row[5]*100)),
            'generic' => false
    ]);

Using the Eloquent model:updateOrCreate(), if the spreadsheet contains any new items then a new record is added to the product catalogue.

If the Item already exists, but for instance, the price has changed then the existing record is found and updated.

However if the row in the spreadsheet is IDENTICAL to the record already in the database, then Eloquent knows that the record does not have any changes (is not dirty) and skips the write process. In this scenario, the updated_at field retains the previous value.

This might be ok for your use case, but in one project, the updated_at column was being used as part of a scope to show only current products to the user.

The solution is to add updated_at to the data to be written to force a new value, and to ensure that updated_at is in the $fillable array or unguarded.

    Item::updateOrCreate(
        ['code' => $row[0]],
        [
            'description'=> $row[1],
            'category'=> $row[2],
            'uom' => $row[3],
            'case_quantity' => $row[4],
            'each' => intval(strval($row[5]*100)),
            'generic' => false,
            'updated_at' => now(),
    ]);
PreviousAlternatives to using Eloquent AccessorNextUse of lockForUpdate()

Last updated 4 years ago

Was this helpful?