<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Adrian Ocneanu on Medium]]></title>
        <description><![CDATA[Stories by Adrian Ocneanu on Medium]]></description>
        <link>https://medium.com/@aocneanu?source=rss-79d0655bb567------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*E8sU1lzCEGtUCoNa.</url>
            <title>Stories by Adrian Ocneanu on Medium</title>
            <link>https://medium.com/@aocneanu?source=rss-79d0655bb567------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 22:45:05 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@aocneanu/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Laravel: How to use optimistic lock for model updates with a simple Trait]]></title>
            <link>https://medium.com/@aocneanu/how-to-use-optimistic-lock-in-laravel-with-a-simple-trait-eaa002f4095?source=rss-79d0655bb567------2</link>
            <guid isPermaLink="false">https://medium.com/p/eaa002f4095</guid>
            <category><![CDATA[traits]]></category>
            <category><![CDATA[versioning]]></category>
            <category><![CDATA[optimistic-lock]]></category>
            <category><![CDATA[laravel]]></category>
            <dc:creator><![CDATA[Adrian Ocneanu]]></dc:creator>
            <pubDate>Fri, 25 May 2018 15:47:35 GMT</pubDate>
            <atom:updated>2018-05-25T16:03:52.625Z</atom:updated>
            <content:encoded><![CDATA[<p>Recently I had the opportunity to (virtually) meet a very determined customer that raised the problem of concurrency control when two users are altering the same data in a Laravel application.</p><p>In a few words, the concurrency problem happens when a user retrieves some information from the database, a second user alters that information, just before the first user tries to alter it too. In some cases this would not be critical, but there are situations where you don’t want to allow the first user to “make a decision” based on stale information.</p><p>There may be two scenarios when the first user can be “affected” by the second user’s actions.</p><p>Scenario 1: destroy</p><p>Step 1. User One retrieves a model in an edit form.</p><p>Step 2. User Two deletes the same model</p><p>Step 3. User One attempts to update the model.</p><p>Once the update route is accessed, (assuming that route-model binding is used) Laravel will throw a NotFoundHttpException with a “No query results for model [App\Model]” message. So there are no critical issues here</p><p>Scenario 2: update</p><p>Step 1. User One retrieves a model in an edit form.</p><p>Step 2. User Two updates the same model</p><p>Step 3. User One attempts to update the model again.</p><p>What would one expect to happen in this case? Laravel will allow updating the model with the attributes received from User Two with no questions asked and maybe that’s not ideal…</p><p>This can be avoided by using a version column in the model’s table, where the version of each model’s state is stored.</p><p>When the model is created, the version should be set to 1. Whenever the model is retrieved it will also have its version, and if an attempt to update is made, the model’s version from the request can be easily compared with the actual version from the from the database. If the versions match, then the version is incremented and model is updated. But if the versions don’t match, it means that someone updated the model in between and an exception should be thrown.</p><p>I wanted to find a clean implementation using as much as I could from what Laravel already provides out of the box to overcome this in an automated way.</p><p>Fortunately, Laravel has Eloquent, and Eloquent models fire events on all the relevant points in their lifecycle, so I decided to use that.</p><p>I created a “Versioning” Trait that uses the magic boot[TraitName] method (you can find more about this <a href="https://www.archybold.com/blog/post/booting-eloquent-model-traits">here</a>). By default, the trait assumes that a version column is present in the model’s table. But if the developer wishes to use another column for versioning, the trait allows that by using protected $versioningAttribute = ‘customVersionAttr’ on the model (indicating the proper column).</p><p>Further, the Trait listens to the creatingand the updating events on the target model. When creating is fired (just before the model is created), the version attribute is set to <strong>1</strong>. And then when updating is fired (just before the actual update), the version from the edited model is compared with the version from the database and if the versions don’t match, a ConflictHttpException is thrown.</p><p>This way when versioning is required for a model everything is handled under the hood when using the Trait.</p><p>The code is open source and can be seen on <a href="https://github.com/laravel-enso/versioning">github</a></p><p>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=eaa002f4095" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>