<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Kodeas</title>
    <description>The latest articles on DEV Community by Kodeas (@kodeas).</description>
    <link>https://dev.to/kodeas</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Forganization%2Fprofile_image%2F4422%2Fa47d6fd5-4d80-4e06-9a07-833e6f46bf04.png</url>
      <title>DEV Community: Kodeas</title>
      <link>https://dev.to/kodeas</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kodeas"/>
    <language>en</language>
    <item>
      <title>Dealing with currency in Laravel</title>
      <dc:creator>Salih</dc:creator>
      <pubDate>Sun, 22 May 2022 12:17:03 +0000</pubDate>
      <link>https://dev.to/kodeas/dealing-with-currency-in-laravel-32i</link>
      <guid>https://dev.to/kodeas/dealing-with-currency-in-laravel-32i</guid>
      <description>&lt;p&gt;Most applications have to deal with currencies in some way or another and unfortunately it isn't always straightforward due to database limitations. &lt;/p&gt;

&lt;p&gt;Among a few different solutions to how to store currency in the database my choice always is storing it in cents as an integer. &lt;em&gt;(if you prefer another way turn around now)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Storing in cents solves many of our problems like dealing with floats and such, however, creates a new challenge, &lt;strong&gt;remembering what state your variables are in&lt;/strong&gt;. The amount of times I would by mistake store a currency in USD format instead or converted cents to cents again is way too damn high. Also converting constantly back and forth gets quite old quickly and &lt;strong&gt;don't we all hate writing the same thing over and over again?&lt;/strong&gt;&lt;br&gt;
Let me demonstrate what I am talking about.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$currency&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nv"&gt;$currencyInCents&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$currency&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nv"&gt;$someFeeRate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.12&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nv"&gt;$currencyInCentsWithFee&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$currencyInCents&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nv"&gt;$someFeeRate&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nv"&gt;$model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Model&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
  &lt;span class="s1"&gt;'amount'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$currencyInCentsWithFee&lt;/span&gt;
&lt;span class="p"&gt;]);&lt;/span&gt;

&lt;span class="c1"&gt;// $model-&amp;gt;amount - &lt;/span&gt;
&lt;span class="c1"&gt;//I already lost what format this was in may be I have a bad memory&lt;/span&gt;

&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;response&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
  &lt;span class="s1"&gt;'currency'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s1"&gt;'$'&lt;/span&gt;&lt;span class="mf"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;number_format&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$model&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Did you realise how many times I had to write &lt;code&gt;"inCents"&lt;/code&gt; within my variables? &lt;br&gt;
Of course I am exaggerating a little bit with this example but once there more levels of calculations things get worse and worse &lt;em&gt;if you have been there, you know&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;There is also a good chance that the next developer within the project is going to do something completely different to what I just did.&lt;/p&gt;

&lt;p&gt;As a solution we came up with a simple package for currency:&lt;br&gt;
&lt;a href="https://github.com/Kodeas/currency"&gt;https://github.com/Kodeas/currency&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$currency&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Currency&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;fromUsd&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nv"&gt;$someFeeRate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.12&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nv"&gt;$currencyWithFee&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Currency&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;fromCents&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$currency&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;inCents&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nv"&gt;$someFeeRate&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;//model has currency as a cast of Currency::class&lt;/span&gt;
&lt;span class="nv"&gt;$model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Model&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
  &lt;span class="s1"&gt;'amount'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$currencyWithFee&lt;/span&gt;
&lt;span class="p"&gt;]);&lt;/span&gt;

&lt;span class="c1"&gt;//$model-&amp;gt;amount - &lt;/span&gt;
&lt;span class="c1"&gt;//this is now an instance of Currency::class so it is in whatever format you wish&lt;/span&gt;

&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;response&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;
  &lt;span class="s1"&gt;'currency'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$model&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;toReadable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'$'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;currency&lt;/code&gt; class is used as a cast to standardise currency operations in your entire app, regardless of who is the developer writing it. Your currency will always be stored in cents and you will never forget what state your currency was during your operations.&lt;/p&gt;

&lt;p&gt;I also appreciate you might be sceptical of adding a package for something so simple therefore, I also &lt;a href="https://dev.to/kodeas/creating-casts-like-carbon-in-laravel-3k49"&gt;wrote&lt;/a&gt; about how it was made so you can also &lt;strong&gt;create one of your own&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;There it is, nice and quick solution to most of your currency issues while using Laravel.&lt;/p&gt;

&lt;p&gt;Please feel free to contribute to the package if you have any additions and show some ❤️ with a like if this helped you.&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>php</category>
      <category>opensource</category>
    </item>
    <item>
      <title>Creating casts like Carbon in Laravel</title>
      <dc:creator>Salih</dc:creator>
      <pubDate>Sat, 21 May 2022 15:06:00 +0000</pubDate>
      <link>https://dev.to/kodeas/creating-casts-like-carbon-in-laravel-3k49</link>
      <guid>https://dev.to/kodeas/creating-casts-like-carbon-in-laravel-3k49</guid>
      <description>&lt;p&gt;Most of us using Laravel have used &lt;strong&gt;Carbon&lt;/strong&gt; for date casting since it comes with the package and also simply because it is just awesome. &lt;/p&gt;

&lt;p&gt;What if you wanted to &lt;strong&gt;create a similar experience for your own custom casts&lt;/strong&gt;?&lt;/p&gt;

&lt;p&gt;So, here is a short guide how to achieve something similar:&lt;br&gt;
&lt;em&gt;(I will use Currency as an example however you can apply this to anything)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Firstly, we will need our trusty type class with a simple constructor and a a couple static initiators (just like &lt;code&gt;Carbon::parse('05/05/22')&lt;/code&gt;). Also it would be great to have some getters like &lt;code&gt;toUsd()&lt;/code&gt; and &lt;code&gt;toCents()&lt;/code&gt; for &lt;strong&gt;ease of use&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Currency&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;__construct&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nv"&gt;$amountInCents&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;amountInCents&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$amountInCents&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;fromCents&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nv"&gt;$amountInCents&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;&lt;span class="kt"&gt;Currency&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;self&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$amountInCents&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;fromUsd&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;float&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;int&lt;/span&gt; &lt;span class="nv"&gt;$amount&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kt"&gt;Currency&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;self&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$amount&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;toUsd&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;int&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;amountInCents&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;toCents&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;amountInCents&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we are able to initiate our class using &lt;code&gt;Currency::fromCents(100)&lt;/code&gt;, I prefer to store all my currencies in cents since floats are weird in database level but let's not get into the nitty gritty here.&lt;/p&gt;

&lt;p&gt;Secondly, we will utilize Laravel's &lt;strong&gt;custom casts&lt;/strong&gt; feature for this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Currency&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;CastsAttributes&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="cd"&gt;/**
     * Cast the given value.
     *
     * @param  Model  $model
     * @param  string  $key
     * @param  mixed  $value
     * @param  array  $attributes
     * @return CurrencyType
     */&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$model&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nv"&gt;$key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;mixed&lt;/span&gt; &lt;span class="nv"&gt;$value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;array&lt;/span&gt; &lt;span class="nv"&gt;$attributes&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kt"&gt;CurrencyType&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;CurrencyType&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;fromCents&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="cd"&gt;/**
     * @param  Model  $model
     * @param  string  $key
     * @param  CurrencyType|string|float|int  $value
     * @param  array  $attributes
     * @return int
     * @throws InvalidCurrencyFormatException
     */&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$model&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nv"&gt;$key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;mixed&lt;/span&gt; &lt;span class="nv"&gt;$value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;array&lt;/span&gt; &lt;span class="nv"&gt;$attributes&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$value&lt;/span&gt; &lt;span class="k"&gt;instanceof&lt;/span&gt; &lt;span class="nc"&gt;CurrencyType&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nv"&gt;$currency&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;CurrencyType&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;class&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="nv"&gt;$type&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;getType&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;InvalidCurrencyFormatException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"The given value must be &lt;/span&gt;&lt;span class="nv"&gt;$currency&lt;/span&gt;&lt;span class="s2"&gt;. &lt;/span&gt;&lt;span class="nv"&gt;$type&lt;/span&gt;&lt;span class="s2"&gt; was given."&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nv"&gt;$value&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;toCents&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Please have a look at the &lt;a href="https://laravel.com/docs/9.x/eloquent-mutators#custom-casts"&gt;docs&lt;/a&gt; for more info about this step&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;We now have our shiny &lt;code&gt;Currency&lt;/code&gt; cast that we can use on any model like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;'money_column' =&amp;gt; Currency::class,
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I am sure we are all aware of how magical🪄 casts are but let me outline we achieved here.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Whenever you want to store this column you can just use a &lt;code&gt;Currency&lt;/code&gt; object and just send it over to the database and &lt;strong&gt;it will automatically to store it in cents&lt;/strong&gt; without questions.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Whenever you fetch this record from the database, &lt;strong&gt;it will automatically be in the format of &lt;code&gt;Currency&lt;/code&gt;&lt;/strong&gt; and you can use the getters as you wish.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;Now you can stop at this point and call it a day, I wouldn't blame you at all but we are here to have the full &lt;code&gt;Carbon&lt;/code&gt; style cast experience. So lets go a little bit further, you won't regret it I am sure (I hope you won't).&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Our next step is a beautiful detail that most of us take for granted about Carbon.&lt;em&gt;(at least I did until recently)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;When a Carbon object is passed to front-end (blade or json) it magically turns into a formatted string instead of an Object.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let's add this to our &lt;code&gt;Currency&lt;/code&gt; class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;JsonSerializable&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Currency&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;JsonSerializable&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nv"&gt;$amountInCents&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;__construct&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nv"&gt;$amountInCents&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;amountInCents&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$amountInCents&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;fromCents&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nv"&gt;$amountInCents&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kt"&gt;Currency&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;self&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$amountInCents&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;fromUsd&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;float&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;int&lt;/span&gt; &lt;span class="nv"&gt;$amount&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kt"&gt;Currency&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;self&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$amount&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;toUsd&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="kt"&gt;float&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;int&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;amountInCents&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;toCents&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;amountInCents&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;toReadable&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;number_format&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;toUsd&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;jsonSerialize&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;toReadable&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;__toString&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;toReadable&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;JsonSerializable is a native php interface for more info have a look at &lt;a href="https://www.php.net/manual/en/class.jsonserializable.php"&gt;php docs&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Using &lt;code&gt;_JsonSerializable&lt;/code&gt; interface makes sure whenever you class is going through a &lt;code&gt;json_encode()&lt;/code&gt; the &lt;code&gt;jsonSerialize(&lt;/code&gt; function is used and Laravel almost always runs any response through &lt;code&gt;json_encode()&lt;/code&gt; within the &lt;code&gt;ResponseFactory&lt;/code&gt; class.&lt;br&gt;
&lt;em&gt;I have to admit it took me way too long to figure this out&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;I also added the &lt;code&gt;__toString()&lt;/code&gt; method so if at any point you would like to use this class as a string you wouldn't need any other helpers.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$currency&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Currency&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;fromUsd&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nb"&gt;json_encode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$currency&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//"1.00"&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$currency&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;//"1.00"&lt;/span&gt;

&lt;span class="mf"&gt;...&lt;/span&gt;
&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;response&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="s1"&gt;'amount'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$currency&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="c1"&gt;//would be "1.00" on the front-end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I think that is just elegant, all my worries about storing currency in cents or usd are gone. &lt;/p&gt;

&lt;p&gt;If you have any other use-cases for something like this I would love to see it in the comments, I am sure there must be many.&lt;/p&gt;

&lt;p&gt;Thanks for reading, drop a ❤️ if you made it all the way down here.&lt;/p&gt;

&lt;p&gt;Also let me know in the comments if you have any further improvements to this or found this useful or just plain hate the idea 😬&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>php</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Executing Shell Commands In Laravel</title>
      <dc:creator>Bertug Korucu</dc:creator>
      <pubDate>Wed, 17 Nov 2021 21:45:20 +0000</pubDate>
      <link>https://dev.to/kodeas/executing-shell-commands-in-laravel-1098</link>
      <guid>https://dev.to/kodeas/executing-shell-commands-in-laravel-1098</guid>
      <description>&lt;p&gt;Both &lt;code&gt;shell_exec()&lt;/code&gt; and &lt;code&gt;exec()&lt;/code&gt; do the job - until they don't. &lt;/p&gt;

&lt;p&gt;If your command crash for some reason, you won't know -  &lt;strong&gt;&lt;code&gt;shell_exec()&lt;/code&gt; and &lt;code&gt;exec()&lt;/code&gt; don't throw Exceptions.&lt;/strong&gt; They just fail silently. 😱&lt;/p&gt;

&lt;p&gt;So, here is my solution to encounter this problem:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;use Symfony\Component\Process\Process;

class ShellCommand
{
    public static function execute($cmd): string
    {
        $process = Process::fromShellCommandline($cmd);

        $processOutput = '';

        $captureOutput = function ($type, $line) use (&amp;amp;$processOutput) {
            $processOutput .= $line;
        };

        $process-&amp;gt;setTimeout(null)
            -&amp;gt;run($captureOutput);

        if ($process-&amp;gt;getExitCode()) {
            $exception = new ShellCommandFailedException($cmd . " - " . $processOutput);
            report($exception);

            throw $exception;
        }

        return $processOutput;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;It utilises Symfony's Process (which comes out of the box to Laravel).&lt;/em&gt; ✨&lt;/p&gt;

&lt;p&gt;With this way, I can throw a custom exception, log the command and the output to investigate, report it to my logs to investigate, etc.&lt;/p&gt;

&lt;p&gt;No more failing silently 😇&lt;/p&gt;

&lt;p&gt;Hope you like this little piece! If you do, please give it a ❤️&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>php</category>
    </item>
    <item>
      <title>Bulk Update Multiple Records with Separate Data — Laravel</title>
      <dc:creator>Bertug Korucu</dc:creator>
      <pubDate>Sun, 11 Jul 2021 23:45:04 +0000</pubDate>
      <link>https://dev.to/kodeas/bulk-update-multiple-records-with-separate-data-laravel-4j7k</link>
      <guid>https://dev.to/kodeas/bulk-update-multiple-records-with-separate-data-laravel-4j7k</guid>
      <description>&lt;p&gt;As a rule of thumb, we should never run database queries inside a for-loop!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;“Database transaction” is an expensive operation.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For example, let’s say we designed an inventory software and it’s being used in production for a year, and reached 1,000,000 transactions.&lt;/p&gt;

&lt;p&gt;Suddenly, we learnt that we didn’t add the VAT to our transactions. For the transactions in the future, it’s pretty easy to deal with, maybe with a mutator.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Transaction extends Model { 
    public $vat = 0.20;

    public function setPriceAttribute($value) {
        $this-&amp;gt;attributes['price'] += $value * $this-&amp;gt;vat;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Future records are pretty easy to deal with. However how are we going to edit the 1 million records from the past.&lt;/p&gt;

&lt;p&gt;For editing data from the past, I prefer to create a Seeder.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;php artisan make:seeder AddVatToTransactions&lt;/code&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  How not to do it?
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz4zndxs8ysb5s7fbw5r4.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz4zndxs8ysb5s7fbw5r4.jpeg" alt="Drake says no" width="451" height="451"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class AddVatToTransactions extends Seeder {

  public function run() 
  {
    $vat = 0.20;
    $transactions = Transaction::get();

    foreach ($transactions as $transaction) {
       $transaction-&amp;gt;price += $transaction-&amp;gt;price * $vat
       $transaction-&amp;gt;save();
    }
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, running it in a loop of 1 million and making a “database transaction” in each iteration of the loop — not a good idea! (Spoiler Alert: It’ll freeze your system 😀)&lt;/p&gt;




&lt;h4&gt;
  
  
  Then, how to do it?
&lt;/h4&gt;

&lt;p&gt;Again, in our &lt;code&gt;AddVatToTransactions&lt;/code&gt; Seeder:&lt;/p&gt;

&lt;p&gt;The idea in mysql query is “CASE Statements”&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;UPDATE db.transactions
SET PRICE = CASE  
              WHEN id = 3 THEN 500
              WHEN id = 4 THEN 300
           END 
WHERE ID IN (3, 4)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Now, let’s do it in Laravel:
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$vat = 0.20;
$transactions = Transaction::get();

$cases = [];
$ids = [];
$params = [];

foreach ($transactions as $transaction) {
    $cases[] = "WHEN {$transaction-&amp;gt;id} then ?";
    $params[] = $transaction-&amp;gt;profit * $vat;
    $ids[] = $transaction-&amp;gt;id;
}

$ids = implode(',', $ids);
$cases = implode(' ', $cases);

if (!empty($ids)) {
    \DB::update("UPDATE transactions SET `price` = CASE `id` {$cases} END WHERE `id` in ({$ids})", $params);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will make one database transaction to write it all your updates.⚡️&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fiq7j5qnkh4ejehqx0lme.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fiq7j5qnkh4ejehqx0lme.jpeg" alt="Drake says yes" width="263" height="192"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;🗣 I can hear some of you saying: “It’s still FREEZING”&lt;/p&gt;

&lt;h3&gt;
  
  
  So.. Optimizing it even further:
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;#1: “Select” only the data you need from the database to consume less RAM.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In our example, we only use “id” and “price” columns. So let’s only select them.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$transactions = Transaction::select('id', 'price')-&amp;gt;get();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;#2: “Chunk” your collection to separate your transaction to multiple database transactions.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In Laravel, you can chunk collections like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Transaction::get()-&amp;gt;chunk(5000);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Let’s apply all in our example
&lt;/h3&gt;

&lt;p&gt;Here, first we divide our &lt;code&gt;$transactions&lt;/code&gt; collection into 5000 chunks and we do a “database transaction” per 5k records at once.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$vat = 0.20;
$transactions = Transaction::get();

foreach ($transactions-&amp;gt;chunk(5000) as $chunk) {
   $cases = [];
   $ids = [];
   $params = [];

   foreach ($chunk as $transaction) {
       $cases[] = "WHEN {$transaction-&amp;gt;id} then ?";
       $params[] = $transaction-&amp;gt;profit * $vat;
       $ids[] = $transaction-&amp;gt;id;
   }

   $ids = implode(',', $ids);
   $cases = implode(' ', $cases);

   if (!empty($ids)) {
       \DB::update("UPDATE transactions SET `price` = CASE `id` {$cases} END WHERE `id` in ({$ids})", $params);
   }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Hope you like this trick!&lt;/p&gt;

&lt;p&gt;Please let me know what you think in the comments 💬&lt;/p&gt;

&lt;p&gt;Happy coding! 😊&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>mysql</category>
    </item>
    <item>
      <title>Using Vite with Inertia — Laravel, Vue &amp; Tailwind</title>
      <dc:creator>Bertug Korucu</dc:creator>
      <pubDate>Sun, 11 Jul 2021 23:24:57 +0000</pubDate>
      <link>https://dev.to/kodeas/using-vite-with-inertia-laravel-vue-tailwind-2h5k</link>
      <guid>https://dev.to/kodeas/using-vite-with-inertia-laravel-vue-tailwind-2h5k</guid>
      <description>&lt;p&gt;&lt;strong&gt;TLDR; 5800ms (Laravel Mix) to 224ms (Vite⚡️) on hot-reload!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We’ve been using Laravel-Mix for years and it’s been doing pretty good. However, recently we decided to build a project using Inertia.js (which was an awesome decision).&lt;/p&gt;

&lt;p&gt;As the project started to grow, the development became a pain to wait for the webpack compiling.&lt;/p&gt;

&lt;p&gt;Then, we decided to give &lt;a href="https://vitejs.dev"&gt;Vite&lt;/a&gt; (from Vue’s creator Evan You) and the results were… ASTONISHING! I’ve been seeing Vite around in Twitter, but to be honest with you, I was not expecting THIS MUCH OF SPEED! 🚀&lt;/p&gt;

&lt;h3&gt;
  
  
  Laravel-Mix was getting too so slow. 🐢
&lt;/h3&gt;

&lt;p&gt;Benchmark Test on Hot Reloading &lt;em&gt;(with 16" MBP 64gb ram, 2.4 GHz 8-Core Intel Core i9)&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;|    Compilation     | Laravel Mix | Vite  |
|--------------------|-------------|-------|
| Initial Compile    | 13257ms     | 636ms |
| Change Reflection  | 5848ms      | 224ms |
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That’s like ‘20x for initial compilation’ and ‘25x when code change’ 😲&lt;/p&gt;

&lt;p&gt;We are fascinated by results, so let me tell you how to set it up, so you can try it out too.&lt;/p&gt;

&lt;h3&gt;
  
  
  Migrating To Vite⚡️
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;First, you’ll need to install Vite:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install vite
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Then, install Tailwind
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i tailwindcss postcss autoprefixer -D
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Create “vite.config.js” and “postcss.config.js” in your project base
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const { resolve } = require('path');
import vue from '@vitejs/plugin-vue';

export default ({ command }) =&amp;gt; ({
  base: command === 'serve' ? '' : '/dist/',
  publicDir: 'fake_dir_so_nothing_gets_copied',
  build: {
    manifest: true,
    outDir: resolve(__dirname, 'public/dist'),
    rollupOptions: {
      input: 'resources/js/app.js',
    },
  },

  plugins: [vue()],

  resolve: {
    alias: {
      '@': resolve('./resources/js'),
    },
  },
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;module.exports = {
  plugins: {
    tailwindcss: {},
    autoprefixer: {},
  }, 
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;For the sake of completeness, here is Tailwind config (JIT is amazing as well!)&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;module.exports = {
  mode: "jit",
  purge: ['./resources/**/*.{js,jsx,ts,tsx,vue,blade.php}'],
  theme: {},
  variants: {},
  plugins: [],
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;And finally, you need to configure your app.js for Vite to work with Inertia.js. (The production compiling part kept me in the dark for a few hours)
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import 'vite/dynamic-import-polyfill';
import '../css/app.css';
import { createApp, h } from 'vue'
import { App, plugin } from '@inertiajs/inertia-vue3'

let asyncViews = () =&amp;gt; {
  return import.meta.glob('./Pages/**/*.vue');
}

const app = createApp({
  render: () =&amp;gt; h(App, {
    initialPage: JSON.parse(el.dataset.page),
        resolveComponent: async name =&amp;gt; {
            if (import.meta.env.DEV) {
                return (await import(`./Pages/${name}.vue`)).default;
            } else {
                let pages = asyncViews();
                const importPage = pages[`./Pages/${name}.vue`];
                return importPage().then(module =&amp;gt; module.default);
            }
        }
    })
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Few things to keep in mind are:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;You can’t use &lt;code&gt;require("file")&lt;/code&gt; syntax, so you always need to use &lt;code&gt;import * from file.js&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You need to specify file extensions when importing Vue components, like &lt;code&gt;import FormInput from "@/Shared/Form/FormInput.vue"&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;"app.js" is the only point of entry for your app, so you need to import your app.css file in your app.js.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;…and your front-end is ready 🎉&lt;/strong&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  Setting up Laravel and package.json scripts
&lt;/h3&gt;

&lt;p&gt;I wanted to run “hot reloading” as well as “production” environment interchangeably in my local, so I came up with the below solution. (If you figure out a better way, I’d be happy to hear)&lt;/p&gt;

&lt;p&gt;Vite in 'dev mode' creates a local server in &lt;a href="https://localhost:3000"&gt;https://localhost:3000&lt;/a&gt; (which can be configured in vite.config.js) whereas in ‘production mode’, it creates files in 'public/dist'.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Edit your ‘package.json’ file accordingly:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"scripts": {
    "predev": "printf \"dev\" &amp;gt; public/hot",
    "dev": "vite",
    "preprod": "printf \"prod\" &amp;gt; public/hot",
    "prod": "vite build"
},
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;npm run vite&lt;/code&gt; is hot reloading itself and npm run dev is just for alias. The “pre” hooks are used to create a file in public directory so that the backend can figure out which mode is running.&lt;/p&gt;

&lt;p&gt;Finally, you need to create a helper to resolve the path in your blade &lt;br&gt;
— just like Laravel Mix’s &lt;code&gt;{{ mix('/js/app.js') }}&lt;/code&gt; helper.&lt;/p&gt;

&lt;p&gt;You can create this php file in 'app/Helpers/vite.php' (or anywhere you like)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php

use Illuminate\Support\HtmlString;

if (! function_exists('vite_assets')) {
    /**
     * @return HtmlString
     * @throws Exception
     */
    function vite_assets(): HtmlString
    {
        $devServerIsRunning = false;

        if (app()-&amp;gt;environment('local')) {
            try {
                $devServerIsRunning = file_get_contents(public_path('hot')) == 'dev';
            } catch (Exception) {}
        }

        if ($devServerIsRunning) {
            return new HtmlString(&amp;lt;&amp;lt;&amp;lt;HTML
            &amp;lt;script type="module" src="http://localhost:3000/@vite/client"&amp;gt;&amp;lt;/script&amp;gt;
            &amp;lt;script type="module" src="http://localhost:3000/resources/js/app.js"&amp;gt;&amp;lt;/script&amp;gt;
        HTML);
        }
        $manifest = json_decode(file_get_contents(
            public_path('dist/manifest.json')
        ), true);
        return new HtmlString(&amp;lt;&amp;lt;&amp;lt;HTML
        &amp;lt;script type="module" src="/dist/{$manifest['resources/js/app.js']['file']}"&amp;gt;&amp;lt;/script&amp;gt;
        &amp;lt;link rel="stylesheet" href="/dist/{$manifest['resources/js/app.js']['css'][0]}"&amp;gt;
    HTML);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;And include it to your &lt;code&gt;composer.json&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"autoload": {
    "psr-4": {...},
    "files": [
        "app/Helpers/vite.php"
    ]
},
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;[make sure to run:&lt;code&gt;composer dump-autoload&lt;/code&gt;]&lt;/p&gt;

&lt;p&gt;And finally add it to your master.blade.php&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html&amp;gt;
&amp;lt;head&amp;gt;
    &amp;lt;!-- Stuff --&amp;gt;
    {{ vite_assets() }}
    &amp;lt;!-- More Stuff --&amp;gt;
&amp;lt;/head&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h4&gt;
  
  
  🏁 You are all set. Enjoy the super-speed compiling times ⚡️
&lt;/h4&gt;




&lt;p&gt;I believe this will change your development experience as drastic as it did to me! 🚀&lt;/p&gt;

&lt;p&gt;I’m really curious about your compiling speeds, please leave a comment. 💬&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>vue</category>
      <category>tailwindcss</category>
      <category>inertiajs</category>
    </item>
  </channel>
</rss>
