<?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>Forem: Rafael França</title>
    <description>The latest articles on Forem by Rafael França (@rafaelfranca).</description>
    <link>https://forem.com/rafaelfranca</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%2Fuser%2Fprofile_image%2F416984%2Fc73a46a2-d685-4821-b21e-ec39973d6aa2.jpeg</url>
      <title>Forem: Rafael França</title>
      <link>https://forem.com/rafaelfranca</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/rafaelfranca"/>
    <language>en</language>
    <item>
      <title>Cascading on update (and on delete) in migration</title>
      <dc:creator>Rafael França</dc:creator>
      <pubDate>Sun, 26 Jul 2020 00:19:04 +0000</pubDate>
      <link>https://forem.com/rafaelfranca/cascading-on-update-and-on-delete-in-migration-2jad</link>
      <guid>https://forem.com/rafaelfranca/cascading-on-update-and-on-delete-in-migration-2jad</guid>
      <description>&lt;p&gt;That Laravel’s documentation is rich in content no one can deny, but it does not mean that it has everything it can offer us.&lt;/p&gt;

&lt;p&gt;An example is when we need to perform a cascading update, some indicate the use of static methods in the model. But, there is a simpler way that few developers know, and is not explicit in the documentation.&lt;/p&gt;

&lt;h1&gt;
  
  
  On update
&lt;/h1&gt;

&lt;p&gt;An example how our code would look like:&lt;/p&gt;

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

Schema::create('posts', function (Blueprint $table) {
    $table-&amp;gt;increments('id');
    $table-&amp;gt;integer('author')-&amp;gt;unsigned();
    $table-&amp;gt;timestamps();
    $table-&amp;gt;foreign('author')-&amp;gt;references('id')-&amp;gt;on('users')-&amp;gt;onUpdate('cascade');
});


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Now, our cascading on update it’s in the migration.&lt;/p&gt;
&lt;h1&gt;
  
  
  On delete
&lt;/h1&gt;

&lt;p&gt;If I want to cascade on delete?&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

Schema::create('posts', function (Blueprint $table) {
    $table-&amp;gt;increments('id');
    $table-&amp;gt;integer('author')-&amp;gt;unsigned();
    $table-&amp;gt;timestamps();
    $table-&amp;gt;foreign('author')-&amp;gt;references('id')-&amp;gt;on('users')-&amp;gt;onDelete('cascade');
});


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;It's easy, right? Let's avance.&lt;/p&gt;
&lt;h1&gt;
  
  
  Combine On update and On delete
&lt;/h1&gt;

&lt;p&gt;If, I want to use on update and on delete in the same column? Yes, you can!&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

Schema::create('posts', function (Blueprint $table) {
    $table-&amp;gt;increments('id');
    $table-&amp;gt;integer('author')-&amp;gt;unsigned();
    $table-&amp;gt;timestamps();
    $table-&amp;gt;foreign('author')-&amp;gt;references('id')-&amp;gt;on('users')-&amp;gt;onDelete('cascade')-&amp;gt;onUpdate('cascade');
});


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h1&gt;
  
  
  Changes on Laravel 7
&lt;/h1&gt;

&lt;p&gt;In your 7th version, Laravel bring us some new use case:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

Schema::create('posts', function (Blueprint $table) {
    $table-&amp;gt;id();
    $table-&amp;gt;foreignId('author')-&amp;gt;constrained('users')-&amp;gt;onDelete('cascade')-&amp;gt;onUpdate('cascade');
    $table-&amp;gt;timestamps();
});


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Let me explain the changes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;id()&lt;/code&gt; it's the same of &lt;code&gt;bigIncrements('id')&lt;/code&gt;. If you want to change the column name from &lt;code&gt;id&lt;/code&gt; to &lt;code&gt;cid&lt;/code&gt;, for eg., you can use &lt;code&gt;id('cid')&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;foreignId('author')&lt;/code&gt; it's the same of &lt;code&gt;unsignedBigInteger('author')&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;constrained('users')&lt;/code&gt; it's the same of &lt;code&gt;references('id')-&amp;gt;on('users')&lt;/code&gt;, that by default references the &lt;code&gt;id&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Advanced
&lt;/h2&gt;

&lt;p&gt;By default, &lt;code&gt;constrained()&lt;/code&gt; references the &lt;code&gt;id&lt;/code&gt;, but you can change this, passing a second param, for eg., &lt;code&gt;constrained('users', 'cid')&lt;/code&gt;, in this case, it's the same of &lt;code&gt;references('cid')-&amp;gt;on('users')&lt;/code&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  More advanced
&lt;/h2&gt;

&lt;p&gt;By default, &lt;code&gt;constrained()&lt;/code&gt; not need the first param, if you use the default structure of database (tables name in plural and in english), and your &lt;code&gt;foreignId()&lt;/code&gt; is like this &lt;code&gt;foreignId('user_id')&lt;/code&gt;, you can use only &lt;code&gt;$table-&amp;gt;foreignId('author')-&amp;gt;constrained()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;From behind, Laravel get the your &lt;code&gt;foreignId&lt;/code&gt; that's &lt;code&gt;user_id&lt;/code&gt;, and select all before the last undeline, in our eg, &lt;code&gt;user&lt;/code&gt;, and apply the plural, that convert in &lt;code&gt;users&lt;/code&gt;, it's the name of table. And, all after last underline will be the name of the column.&lt;/p&gt;

&lt;p&gt;In the end, &lt;code&gt;foreignId('user_id')-&amp;gt;constrained()&lt;/code&gt;, it's the same of &lt;code&gt;foreign('user_id')-&amp;gt;references('id')-&amp;gt;on('users')&lt;/code&gt;, even as &lt;code&gt;foreignId('user_cid')-&amp;gt;constrained()&lt;/code&gt;, it's the same of &lt;code&gt;foreign('user_cid')-&amp;gt;references('cid')-&amp;gt;on('users')&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;What do you think about this tip?&lt;/p&gt;

&lt;p&gt;Soon I back with more! :)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;PS:&lt;/strong&gt; Originally posted on my account on Medium.&lt;/p&gt;


&lt;div class="ltag__link"&gt;
  &lt;a href="https://medium.com/@rafael_franca/laravel-tip-cascading-on-update-in-migration-2100af33081" class="ltag__link__link" rel="noopener noreferrer"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fv2%2Fresize%3Afill%3A88%3A88%2F1%2Aq0RTQvgNZXjRfYwAWv2HCA.jpeg" alt="Rafael França"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="https://medium.com/@rafael_franca/laravel-tip-cascading-on-update-in-migration-2100af33081" class="ltag__link__link" rel="noopener noreferrer"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;Laravel Tip — Cascading on update in migration | by Rafael França | Medium&lt;/h2&gt;
      &lt;h3&gt;Rafael França ・ &lt;time&gt;May 22, 2018&lt;/time&gt; ・ 
      &lt;div class="ltag__link__servicename"&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%2Fassets%2Fmedium-f709f79cf29704f9f4c2a83f950b2964e95007a3e311b77f686915c71574fef2.svg" alt="Medium Logo"&gt;
        Medium
      &lt;/div&gt;
    &lt;/h3&gt;
&lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;



</description>
      <category>laravel</category>
      <category>eloquent</category>
      <category>protips</category>
      <category>php</category>
    </item>
    <item>
      <title>Attribute Casting with Laravel</title>
      <dc:creator>Rafael França</dc:creator>
      <pubDate>Sat, 25 Jul 2020 21:40:41 +0000</pubDate>
      <link>https://forem.com/rafaelfranca/attribute-casting-with-laravel-1h5j</link>
      <guid>https://forem.com/rafaelfranca/attribute-casting-with-laravel-1h5j</guid>
      <description>&lt;p&gt;When was developing an application that use the MVC pattern, we need define our model, that is the layer that communicate our application with the database.&lt;/p&gt;

&lt;p&gt;But sometimes, we need to casting values that received from model. Let me show an example:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;You’re creating a model to represent Products, and need of: name, value, state (if it’s available or not) and when will it be available.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Every time that you need create, show or edit a product, need to casting the values. Our idea with this article is it makes your job easier, therefore, what about if we pass this responsibility to our model, so that it makes this automatically?&lt;/p&gt;

&lt;p&gt;For this, Laravel has the $casts property. Let’s see how works:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Using this property, the attributes are casted automatically, and you not will need make this manually. Let’s see a practical example.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;You’re create a method to add some days of delay in the availability of a product, for this, hasn’t used a date, only the quantities of days that want to delay.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Without use the $casts, you make something like this:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Note that we used the Carbon package to make a parse of the date and after add the quantities of days that want to be delayed.&lt;/p&gt;

&lt;p&gt;And, if we use $casts, how would it be? Let’s see:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;More simple and cleaner, sure?&lt;/p&gt;

&lt;p&gt;You can think: “OK, I make this but, what I win, beyond save letters that will be typed?”.&lt;/p&gt;

&lt;p&gt;The response to this question is: maintainability. That is, if in the future, you want to know the date and time that a product will be available, only need change your model, changing your $casts from date to datetime. It’s simple&lt;/p&gt;

&lt;p&gt;What do you think about this tip?&lt;/p&gt;

&lt;p&gt;Soon I back with more! :)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;PS:&lt;/strong&gt; Originally posted on my account on Medium.&lt;/p&gt;


&lt;div class="ltag__link"&gt;
  &lt;a href="https://medium.com/@rafaelfrancam/attribute-casting-with-laravel-f62e8a5fbed2" class="ltag__link__link" rel="noopener noreferrer"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fv2%2Fda%3Atrue%2Fresize%3Afill%3A88%3A88%2F0%2Apll3CspUek1Tv-hv" alt="Rafael França"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="https://medium.com/@rafaelfrancam/attribute-casting-with-laravel-f62e8a5fbed2" class="ltag__link__link" rel="noopener noreferrer"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;Attribute Casting with Laravel. When was developing an application that… | by Rafael França | Medium&lt;/h2&gt;
      &lt;h3&gt;Rafael França ・ &lt;time&gt;May 31, 2020&lt;/time&gt; ・ 
      &lt;div class="ltag__link__servicename"&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%2Fassets%2Fmedium-f709f79cf29704f9f4c2a83f950b2964e95007a3e311b77f686915c71574fef2.svg" alt="Medium Logo"&gt;
        Medium
      &lt;/div&gt;
    &lt;/h3&gt;
&lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;


&lt;p&gt;Cover photo by &lt;a href="https://unsplash.com/@ffstop" rel="noopener noreferrer"&gt;Fotis Fotopoulos&lt;/a&gt; on &lt;a href="https://unsplash.com/" rel="noopener noreferrer"&gt;Unsplash&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>protips</category>
      <category>eloquent</category>
      <category>php</category>
    </item>
    <item>
      <title>Dynamic queries with Laravel</title>
      <dc:creator>Rafael França</dc:creator>
      <pubDate>Sat, 25 Jul 2020 21:25:32 +0000</pubDate>
      <link>https://forem.com/rafaelfranca/dynamic-queries-with-laravel-4g4b</link>
      <guid>https://forem.com/rafaelfranca/dynamic-queries-with-laravel-4g4b</guid>
      <description>&lt;p&gt;When developing a project for a client, a doubt arose: how create a dynamic query using only Eloquent?&lt;/p&gt;

&lt;p&gt;Maybe, you can think: “It’s easy! We can use the method when”.&lt;br&gt;
But that wasn’t what I intended, therefore, before continue, let me show you better the problem.&lt;/p&gt;

&lt;p&gt;Imagine that I have a schedule, and I need create an event that start and finish with a difference of weeks and has a repetition in specific days of week. E.g.:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;I, Rafael, intent elevate my English and Spanish level, therefore, intent study English by one hour, in every Sunday, Wednesday and Friday, during a one month; and, Spanish, every Tuesday, Thursday and Saturday.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now, imagine that you receive the dates using a numeric representation, in that 0 is equals a Monday, and 6 is a Saturday, thus we can represent the received data by the vector below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    "frequency": "daily",
    "days": [1, 3, 5],
    "date_start": "29/05/2020",
    "date_end": "29/06/2020",
    "hour_start": "20:00",
    "hour_end": "21:00"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;As every people that want to study, not like be interrupted to make another thing, therefore, we need grant this event not collide with another. So, more one time, how make this, using only Eloquent?&lt;/p&gt;

&lt;p&gt;The response is simple. See above:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[...]

})-&amp;gt;when(request()-&amp;gt;get('frequency') === "daily", function (Builder $subquery) {
    foreach (request()-&amp;gt;get('days') as $day) {
        $subquery-&amp;gt;where('days', 'LIKE', "%$day%");
    }
    return $subquery;
});

[...]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;What do you think about the tip?&lt;/p&gt;

&lt;p&gt;Soon I back with more! :)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;PS:&lt;/strong&gt; Originally posted on my account on Medium.&lt;/p&gt;


&lt;div class="ltag__link"&gt;
  &lt;a href="https://medium.com/@rafaelfrancam/-queries-with-laravel-fb21bf3e0b94" class="ltag__link__link" rel="noopener noreferrer"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fv2%2Fda%3Atrue%2Fresize%3Afill%3A88%3A88%2F0%2Apll3CspUek1Tv-hv" alt="Rafael França"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="https://medium.com/@rafaelfrancam/-queries-with-laravel-fb21bf3e0b94" class="ltag__link__link" rel="noopener noreferrer"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;Dynamic queries with Laravel. When developing a project for a client… | by Rafael França | Medium&lt;/h2&gt;
      &lt;h3&gt;Rafael França ・ &lt;time&gt;May 30, 2020&lt;/time&gt; ・ 
      &lt;div class="ltag__link__servicename"&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%2Fassets%2Fmedium-f709f79cf29704f9f4c2a83f950b2964e95007a3e311b77f686915c71574fef2.svg" alt="Medium Logo"&gt;
        Medium
      &lt;/div&gt;
    &lt;/h3&gt;
&lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;



&lt;p&gt;Cover photo by &lt;a href="https://unsplash.com/@cdr6934" rel="noopener noreferrer"&gt;Chris Ried&lt;/a&gt; on &lt;a href="https://unsplash.com" rel="noopener noreferrer"&gt;Unsplash&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>protips</category>
      <category>eloquent</category>
      <category>php</category>
    </item>
  </channel>
</rss>
