<?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: Abrar Ahmad</title>
    <description>The latest articles on DEV Community by Abrar Ahmad (@abrardev99).</description>
    <link>https://dev.to/abrardev99</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%2F407749%2F6869d60f-f3d3-47fb-8d5b-53958c527227.jpg</url>
      <title>DEV Community: Abrar Ahmad</title>
      <link>https://dev.to/abrardev99</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/abrardev99"/>
    <language>en</language>
    <item>
      <title>Get the Table Name From a Model in Laravel</title>
      <dc:creator>Abrar Ahmad</dc:creator>
      <pubDate>Thu, 25 Apr 2024 14:19:39 +0000</pubDate>
      <link>https://dev.to/abrardev99/get-the-table-name-from-a-model-in-laravel-1132</link>
      <guid>https://dev.to/abrardev99/get-the-table-name-from-a-model-in-laravel-1132</guid>
      <description>&lt;p&gt;Sometimes we need to reference table names throughout our codebase, like when we're using the DB facade.&lt;/p&gt;

&lt;p&gt;Although table names don't change often, I still get an uneasy feeling of hardcoding them. It's so much better to reference the model directly so we have one source of truth for the table name.&lt;/p&gt;

&lt;p&gt;So, let's learn how to do that! We'll also create a trait that allows static access to any of our model table names to keep our code a little tidier.&lt;/p&gt;

&lt;p&gt;If you don't know, Laravel provide a method to get table name from model instance&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="p"&gt;((&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;User&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;getTable&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Above code should return the table name, in our case its &lt;code&gt;users&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;But we don't have new instances always. Its better if we can get the table name statically. &lt;br&gt;
We can get the table name by following. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Add method in model
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&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;getTableName&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="nf"&gt;with&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;static&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;getTable&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;ol&gt;
&lt;li&gt;Get the table name statically
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;getTable&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;// users&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Tip
&lt;/h2&gt;

&lt;p&gt;You can put method in trait and drop it any mode. &lt;/p&gt;

</description>
      <category>laravel</category>
    </item>
    <item>
      <title>Laravel Package for Generating Local Disk Temporary URL</title>
      <dc:creator>Abrar Ahmad</dc:creator>
      <pubDate>Fri, 03 Nov 2023 15:22:02 +0000</pubDate>
      <link>https://dev.to/abrardev99/laravel-package-for-generating-local-disk-temporary-url-4h8d</link>
      <guid>https://dev.to/abrardev99/laravel-package-for-generating-local-disk-temporary-url-4h8d</guid>
      <description>&lt;p&gt;A while ago I wrote an article on how you can generate the temporary URL for a local disk like we do for S3. You can read the full article here  &lt;a href="https://dev.to/abrardev99/laravel-temporary-url-for-local-storage-driver-50of"&gt;https://dev.to/abrardev99/laravel-temporary-url-for-local-storage-driver-50of&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;I needed the same functionality in multiple projects so I ended up adding the same code again and again. So I created a &lt;a href="https://github.com/abrardev99/laravel-local-temporary-url"&gt;package&lt;/a&gt; to achieve temporary URL generation 😍. &lt;/p&gt;

&lt;h2&gt;
  
  
  First Release
&lt;/h2&gt;

&lt;p&gt;Packages come with some nice configurations. &lt;/p&gt;

&lt;p&gt;This is the contents of the published config file:&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="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="s1"&gt;'disk'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'local'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;

    &lt;span class="s1"&gt;'middleware'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'web'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'signed'&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;h2&gt;
  
  
  Usage
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Configuration
&lt;/h3&gt;

&lt;p&gt;This package needs zero configuration, just install and it's good to go. However, if your local disk is different or you want to add another disk, you can configure it. You can add multiple local disks in the config using the &lt;code&gt;disk&lt;/code&gt; key. &lt;br&gt;&lt;/p&gt;

&lt;p&gt;The package applies &lt;code&gt;web&lt;/code&gt; and &lt;code&gt;signed&lt;/code&gt; middleware on routes by default, however, you can configure middleware(s) using the &lt;code&gt;middleware&lt;/code&gt; key.&lt;/p&gt;

&lt;h3&gt;
  
  
  Generate Temporary URL
&lt;/h3&gt;

&lt;p&gt;You can use the same syntax used for S3 disk.&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="nc"&gt;Storage&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;disk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'local'&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;temporaryUrl&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'file.txt'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;now&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;addMinutes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>laravel</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Partial Update JSON Column in Postgres</title>
      <dc:creator>Abrar Ahmad</dc:creator>
      <pubDate>Wed, 01 Nov 2023 07:54:19 +0000</pubDate>
      <link>https://dev.to/abrardev99/partial-update-json-column-in-postgres-25e9</link>
      <guid>https://dev.to/abrardev99/partial-update-json-column-in-postgres-25e9</guid>
      <description>&lt;p&gt;Recently, I stumbled upon a use case where I need to partially update the JSON column.&lt;/p&gt;

&lt;h2&gt;
  
  
  What has not worked for me
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Set using -&amp;gt;&amp;gt; operator
&lt;/h3&gt;

&lt;p&gt;I thought I could do something along those lines before realizing it doesn't work 😞&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;UPDATE task SET data-&amp;gt;&amp;gt;'status' = 'completed';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Using json_set and jsonb_set functions
&lt;/h3&gt;

&lt;p&gt;Upon a quick Google search, I found that Postgres has a &lt;code&gt;json_set&lt;/code&gt; function to partially set json values, But I always get the "function does not exist" error. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Rfe1KQzo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6m7f090i0gnwkpojm2hm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Rfe1KQzo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6m7f090i0gnwkpojm2hm.png" alt="function does not exist" width="800" height="94"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What has worked for me
&lt;/h2&gt;

&lt;p&gt;I found a workaround which is using string replacement instead. Take a look at the example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;UPDATE task SET data = replace(data::text, '"status":"pending"', '"status":"completed"')::json;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  How it is working
&lt;/h3&gt;

&lt;p&gt;We are using the Postgres &lt;code&gt;replace&lt;/code&gt; function to replace a string. Postgres supports typecasting. We are converting JSON field to string, replacing the required text, converting it back to JSON, and updating the entire data column with the new values. &lt;/p&gt;

&lt;h2&gt;
  
  
  Note
&lt;/h2&gt;

&lt;p&gt;This works perfectly for my use case but you have to take into consideration eg speed and chances of data inconsistency while replacing the string. &lt;/p&gt;

&lt;p&gt;Enjoy  &lt;/p&gt;

</description>
      <category>postgres</category>
    </item>
    <item>
      <title>Laravel Temporary URL for local storage driver</title>
      <dc:creator>Abrar Ahmad</dc:creator>
      <pubDate>Tue, 19 Apr 2022 09:57:09 +0000</pubDate>
      <link>https://dev.to/abrardev99/laravel-temporary-url-for-local-storage-driver-50of</link>
      <guid>https://dev.to/abrardev99/laravel-temporary-url-for-local-storage-driver-50of</guid>
      <description>&lt;p&gt;Recently, I needed to use a temporary URL. Laravel supports temporary URLs out of the box with S3 drivers. I was using a local disk, but Laravel does not support a temporary URL feature for the local disk. If you are getting this exception, you are on the right article. &lt;br&gt;
&lt;code&gt;RuntimeException with message 'This driver does not support creating temporary URLs.'&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I decided to add the temporary URL logic for my local disk. We'll accomplish this via &lt;a href="https://laravel.com/docs/9.x/urls#signed-urls"&gt;Laravel signed URLs&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The below steps describe how to implement the &lt;code&gt;getTemporaryUrl&lt;/code&gt; function in your Laravel app.&lt;/p&gt;
&lt;h2&gt;
  
  
  Step 1: Add the following code in the AppServiceProvider boot method.
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nc"&gt;Storage&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;disk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'local'&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;buildTemporaryUrlsUsing&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$path&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$expiration&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$options&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="no"&gt;URL&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;temporarySignedRoute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                &lt;span class="s1"&gt;'local.temp'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="nv"&gt;$expiration&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="nb"&gt;array_merge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$options&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'path'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$path&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;Notice that &lt;code&gt;URL::temporarySignedRoute&lt;/code&gt; receives route name &lt;code&gt;local.temp&lt;/code&gt;, which we need to define. &lt;/p&gt;
&lt;h2&gt;
  
  
  Step 2: Add the route in web.php
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nc"&gt;Route&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'local/temp/{path}'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;function&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;$path&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;Storage&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;disk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'local'&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;download&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$path&lt;/span&gt;&lt;span class="p"&gt;);&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;name&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'local.temp'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;This will allow downloading of the file using a temporary URL. &lt;/p&gt;

&lt;p&gt;Now you can easily generate a temporary URL for the local disk.&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="n"&gt;disk&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Storage&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;disk&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'releases-local'&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;$disk&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;temporaryUrl&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;path&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;now&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;addMinutes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  PS
&lt;/h2&gt;

&lt;p&gt;Your feedback is welcome if you want to suggest improvements or know any better way. &lt;/p&gt;

</description>
      <category>laravel</category>
      <category>php</category>
    </item>
    <item>
      <title>Pipeline Pattern in Laravel</title>
      <dc:creator>Abrar Ahmad</dc:creator>
      <pubDate>Sat, 07 Aug 2021 13:41:07 +0000</pubDate>
      <link>https://dev.to/abrardev99/pipeline-pattern-in-laravel-278p</link>
      <guid>https://dev.to/abrardev99/pipeline-pattern-in-laravel-278p</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;PP = Pipeline Pattern&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Today, we will learn the Pipeline Pattern and how to use it in Laravel. You can read in depth about PP at &lt;a href="https://www.cise.ufl.edu/research/ParallelPatterns/PatternLanguage/AlgorithmStructure/Pipeline.htm" rel="noopener noreferrer"&gt;HERE&lt;/a&gt;. PP can be implemented differently in any language but we'll be seeing in PHP &lt;a href="//laravel.com"&gt;Laravel&lt;/a&gt; as Laravel already using this pattern at it's &lt;a href="https://github.com/laravel/framework/blob/dc393980dec1b3ee4b0f36b5b0188859a399b4e4/src/Illuminate/Foundation/Http/Kernel.php#L138" rel="noopener noreferrer"&gt;core&lt;/a&gt;. &lt;/p&gt;

&lt;h2&gt;
  
  
  What is Pipeline Pattern
&lt;/h2&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%2Fw7ek7dushwcdbxh03h7m.png" 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%2Fw7ek7dushwcdbxh03h7m.png" alt="Pipeline Pattern"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Pipeline is a design pattern specifically optimized to handle stepped changes to an object. Think of an assembly line, where each step is a pipe and by the end of the line, you have your transformed object.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;There are vast implementations of PP but to understand easily let's implement a filtering functionality using the pipeline pattern and Laravel. &lt;/p&gt;

&lt;h2&gt;
  
  
  Example
&lt;/h2&gt;

&lt;p&gt;Let's say we are building filtering system in Laravel, we can filter Post by different parameters like is active or not, sort by ASC or DESC. &lt;/p&gt;

&lt;h2&gt;
  
  
  Before Implementing PP.
&lt;/h2&gt;

&lt;h3&gt;
  
  
  PostController.php
&lt;/h3&gt;

&lt;p&gt;In the &lt;code&gt;index&lt;/code&gt; method, we are performing filters. &lt;/p&gt;

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

public function index(Request $request)
    {
        $query = Post::query();

        if ($request-&amp;gt;has('active')) {
            $query-&amp;gt;where('active', $request-&amp;gt;input('active'));
        }

       if ($request-&amp;gt;has('sort')) {
            $query-&amp;gt;orderBy('title', $request-&amp;gt;input('sort'));
        }

        /* get filtered data */
        $posts = $query-&amp;gt;get();

        return view('demo', compact('posts'));
    }


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

&lt;/div&gt;
&lt;h2&gt;
  
  
  Let's convert this mess into beautiful filter pipeline.
&lt;/h2&gt;
&lt;h3&gt;
  
  
  PostController.php
&lt;/h3&gt;

&lt;p&gt;To implement Pipeline pattern, we need to make little refactoring. &lt;code&gt;Index&lt;/code&gt; method will looks like below &lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

public function index(Request $request)
    {
     $posts = app(Pipeline::class)
            -&amp;gt;send(Post::query())
            -&amp;gt;through([
                \App\Filters\Active::class,
                \App\Filters\Sort::class
            ])
            -&amp;gt;thenReturn()
            -&amp;gt;get();

     return view('demo', compact('posts'));
    }


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

&lt;/div&gt;

&lt;p&gt;*&lt;code&gt;send()&lt;/code&gt; method will pass your query to handle method&lt;br&gt;
*&lt;code&gt;through()&lt;/code&gt; method get the parameter as classes to pass through them. In simple words this is the list of filter classes&lt;br&gt;
*&lt;code&gt;thenReturn()&lt;/code&gt; will return the final output&lt;br&gt;
It's all provided by Laravel except we need to create Filter classes which being passed in &lt;code&gt;through()&lt;/code&gt; method. &lt;/p&gt;

&lt;h3&gt;
  
  
  Active class
&lt;/h3&gt;

&lt;p&gt;Create Active class under app/Filters namespace. &lt;/p&gt;


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

&lt;p&gt;namespace App\Filters;&lt;/p&gt;

&lt;p&gt;use Closure;&lt;/p&gt;

&lt;p&gt;class Active&lt;br&gt;
{&lt;br&gt;
    public function handle($request, Closure $next)&lt;br&gt;
    {&lt;br&gt;
        if (! request()-&amp;gt;has('active')) {&lt;br&gt;
            return $next($request);&lt;br&gt;
        }&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    return $next($request)-&amp;amp;gt;where('active', request()-&amp;amp;gt;input('active'));
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;}&lt;/p&gt;

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

&lt;/div&gt;
&lt;h3&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  Sort class&lt;br&gt;
&lt;/h3&gt;

&lt;p&gt;Create Active class under app/Filters namespace. &lt;/p&gt;


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

&lt;p&gt;namespace App\Filters;&lt;/p&gt;

&lt;p&gt;use Closure;&lt;/p&gt;

&lt;p&gt;class Sort&lt;br&gt;
{&lt;br&gt;
    public function handle($request, Closure $next)&lt;br&gt;
    {&lt;br&gt;
        if (! request()-&amp;gt;has('sort')) {&lt;br&gt;
            return $next($request);&lt;br&gt;
        }&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    return $next($request)-&amp;amp;gt;orderBy('title', $request-&amp;amp;gt;input('sort'));
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;}&lt;/p&gt;

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

&lt;/div&gt;
&lt;h3&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  That's it.&lt;br&gt;
&lt;/h3&gt;

&lt;p&gt;Now if I want to add an other filter, I need to make new class let's say &lt;code&gt;Published&lt;/code&gt; and this class in &lt;code&gt;through()&lt;/code&gt; method and implement filter logic in class &lt;code&gt;handle&lt;/code&gt; method. &lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;It might feel overwhelm to implement Pipelines just for two filters but it will be much clean and beneficial for large number of filter or any other complex implementation. &lt;/p&gt;

&lt;h2&gt;
  
  
  Cleaning Filter Classes
&lt;/h2&gt;

&lt;p&gt;PS: We can clean filter classes by extracting common code in base class. Comment below if you want refactoring of Filter classes in next article. &lt;br&gt;
Until next, &lt;br&gt;
Ahmad&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>php</category>
      <category>pipeline</category>
      <category>pattern</category>
    </item>
    <item>
      <title>Peasy way to Show Alerts in Laravel Livewire</title>
      <dc:creator>Abrar Ahmad</dc:creator>
      <pubDate>Wed, 30 Sep 2020 14:32:17 +0000</pubDate>
      <link>https://dev.to/abrardev99/peasy-way-to-show-alerts-in-laravel-livewire-26j</link>
      <guid>https://dev.to/abrardev99/peasy-way-to-show-alerts-in-laravel-livewire-26j</guid>
      <description>&lt;h3&gt;
  
  
  What we will achieve
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/3UAhtuLJG3UBU4EdRu/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/3UAhtuLJG3UBU4EdRu/giphy.gif" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Note
&lt;/h3&gt;

&lt;p&gt;This article assumes that you know the basics knowledge of the &lt;a href="https://laravel.com/"&gt;Laravel&lt;/a&gt; and &lt;a href="https://laravel-livewire.com/"&gt;Livewire package&lt;/a&gt;.  &lt;/p&gt;

&lt;h3&gt;
  
  
  Why
&lt;/h3&gt;

&lt;p&gt;We are going to show Alert messages in Laravel Livewire. If you have little work experience with Livewire you may feel that showing alerts are not easy in the Livewire approach. Also, &lt;a href="https://github.com/mckenziearts/laravel-notify"&gt;Laravel notify&lt;/a&gt; and &lt;a href="https://github.com/realrashid/sweet%20alert"&gt;Laravel Sweetlert&lt;/a&gt; not working. Because these libs are request based while Livewire does not use the traditional Laravel request cycle. &lt;/p&gt;

&lt;h3&gt;
  
  
  Approach
&lt;/h3&gt;

&lt;p&gt;Here I up came with different and easy alerts setup. For alerts, I'm using a javascript &lt;a href="https://github.com/CodeSeven/toastr"&gt;toastr&lt;/a&gt; library. &lt;/p&gt;

&lt;h3&gt;
  
  
  Code
&lt;/h3&gt;

&lt;p&gt;Assuming you have a base layout added necessary javascript and CSS. In the base layout, we have to catch an event dispatched by Livewire, See the script section.  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;app.blade.php&lt;/strong&gt;&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;title&amp;gt;Laravel Livewire Alert&amp;lt;/title&amp;gt;
   &amp;lt;link href="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/toastr.min.css" rel="stylesheet"&amp;gt;
   @livewireStyles
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
 @livewire('addPost')  // adding livewire component 


 @livewireScripts
 &amp;lt;script 
 src="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/lates 
  t/toastr.min.js"&amp;gt;&amp;lt;/script&amp;gt;

&amp;lt;script&amp;gt;
window.addEventListener('alert', event =&amp;gt; { 
             toastr[event.detail.type](event.detail.message, 
             event.detail.title ?? '') toastr.options = {
                    "closeButton": true,
                    "progressBar": true,
                }
            })
 &amp;lt;/script&amp;gt;

&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;add-post.blade.php&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; // other form element 
   ...
 &amp;lt;button type="button" wire:click="save"&amp;gt;Save&amp;lt;/button&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;AddPost.php&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; public function save()
  {
    // validation 
    ...
    // save
    ....

    // show alert
    $this-&amp;gt;dispatchBrowserEvent(
    'alert', ['type' =&amp;gt; 'success',  'message' =&amp;gt; 'Saved']);
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Summing up
&lt;/h3&gt;

&lt;p&gt;So, we have addPost component, in the blade file on save or on any button click method we dispatch a browser event from the Livewire PHP component, &lt;br&gt;
We are catching this component in Javascript from base Layout and showing toaster using toastr library you can use other libraries like &lt;a href="https://sweetalert.js.org/guides/"&gt;SweetAlert&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That's it! Thanks for reading. If you have a question or need source code for the above tutorial please comment below. &lt;/p&gt;

&lt;p&gt;&lt;span&gt;Cover Image by &lt;a href="https://unsplash.com/@imagefactory?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Colin Watts&lt;/a&gt; on &lt;a href="https://unsplash.com/s/photos/alerts?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Unsplash&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>livewire</category>
      <category>javascript</category>
      <category>alerts</category>
    </item>
    <item>
      <title>Question: How to better structure Laravel Livewire code and components?</title>
      <dc:creator>Abrar Ahmad</dc:creator>
      <pubDate>Fri, 24 Jul 2020 18:18:54 +0000</pubDate>
      <link>https://dev.to/abrardev99/question-how-to-better-structure-laravel-livewire-code-and-components-3m6l</link>
      <guid>https://dev.to/abrardev99/question-how-to-better-structure-laravel-livewire-code-and-components-3m6l</guid>
      <description>&lt;p&gt;I'm building an app using &lt;a href="https://laravel-livewire.com/" rel="noopener noreferrer"&gt;Livewire&lt;/a&gt;. If you have some experience with Livewire please take some time to answer. &lt;/p&gt;

&lt;p&gt;I'm trying to keep my livewire components code clean. How we can separate validation logic just like custom Requests in the controller as there is not Request object in the Livewire component. &lt;/p&gt;

&lt;p&gt;My current approach: I make separate method for validation for now? But I'm looking for something much better like custom Requests.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Add extends Component
{
 public function store()
    {
     $this-&amp;gt;Validation();

     $property = new Property();
     $property-&amp;gt;user_id = ...

    }
}

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

&lt;/div&gt;



&lt;p&gt;Also, If is there any better approach for structuring components? My current approach is below: &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%2Fi%2Fot5q78c9etdnd8l6vgtg.png" 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%2Fi%2Fot5q78c9etdnd8l6vgtg.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Any suggestions will be appreciated. &lt;/p&gt;

</description>
      <category>laravel</category>
      <category>livewire</category>
    </item>
    <item>
      <title>Becoming a better Laravel developer by using the SOLID design principles. Part 2</title>
      <dc:creator>Abrar Ahmad</dc:creator>
      <pubDate>Wed, 22 Jul 2020 04:33:40 +0000</pubDate>
      <link>https://dev.to/abrardev99/becoming-a-better-laravel-developer-by-using-the-solid-design-principles-part-2-4nc7</link>
      <guid>https://dev.to/abrardev99/becoming-a-better-laravel-developer-by-using-the-solid-design-principles-part-2-4nc7</guid>
      <description>&lt;p&gt;This article is part 2 of &lt;strong&gt;Becoming a better Laravel developer by using the SOLID design principles&lt;/strong&gt; article series. You can see part one &lt;strong&gt;S - Single-responsibility principle&lt;/strong&gt; &lt;a href="https://dev.to/abrardev99/becoming-a-better-laravel-developer-by-using-the-solid-design-principles-part-1-6f9"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Today, we will discuss the second letter of &lt;strong&gt;SOLID&lt;/strong&gt;   which is &lt;strong&gt;O - Open-closed principle&lt;/strong&gt;. &lt;/p&gt;

&lt;h2&gt;
  
  
  What is O - Open-closed principle
&lt;/h2&gt;

&lt;p&gt;According to the official definition &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;software entities should be open for extension, but closed for modification. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Entities can be classes, modules, functions, etc. The definition meaning that extends functionality by adding new code instead of changing existing code. &lt;br&gt;
It is quite confusing to understand by title. So let's see it with examples in Laravel. &lt;/p&gt;

&lt;p&gt;Let's say we have an e-commerce system that involves payments. So, we have &lt;code&gt;PaymentController&lt;/code&gt; and in that controller, we have &lt;code&gt;pay&lt;/code&gt; method which has two payment methods for now 1) Credit Card 2) Paypal and looks like below:&lt;br&gt;&lt;br&gt;
&lt;code&gt;PaymentController.php&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public function pay(Request $request)
    {
        $payment = new Payment();
        if($type == 'credit'){
            // do credit card payments
            $payment-&amp;gt;paymentWithCreditCard();
        }else {
            //do paypal payments
            $payment-&amp;gt;paymentWithPaypal();
        }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and our &lt;code&gt;Payment&lt;/code&gt; class looks like this: &lt;br&gt;
&lt;code&gt;Payment.php&lt;/code&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 Payment
{

    public function paymentWithCreditCard()
    {
        // logic for credit card payments 
    }

    public function paymentWithPaypal()
    {
        // logic for paypal payments
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What happens when we want to &lt;strong&gt;add a new payment method&lt;/strong&gt; let's say &lt;strong&gt;wire transfer&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In &lt;code&gt;pay&lt;/code&gt; method we add a new if condition and call an appropriate method from the &lt;code&gt;Payment&lt;/code&gt; class.&lt;br&gt;
So the &lt;code&gt;pay&lt;/code&gt; method looks like below:&lt;br&gt;
&lt;code&gt;PaymentController.php&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public function pay(Request $request)
    {
        $payment = new Payment();
        if($type == 'credit'){
            // do credit card payments
            $payment-&amp;gt;paymentWithCreditCard();
        }else if($type == 'paypal') {
            //do paypal payments
            $payment-&amp;gt;paymentWithPaypal();
        }
        else {
            //do wire transfer payments
            $payment-&amp;gt;paymentWithWiretransfer();
        }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and modifying the &lt;code&gt;Payment&lt;/code&gt; class: &lt;br&gt;
&lt;code&gt;Payment.php&lt;/code&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 Payment
{

    public function paymentWithCreditCard()
    {
        // logic for credit card payments
    }

    public function paymentWithPaypal()
    {
        // logic for paypal payments
    }

    public function paymentWithWiretransfer()
    {
        // logic for wire transfer payments
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So what will happen next &lt;strong&gt;What if we have another payment method called Cash on Delivery, we have added another &lt;code&gt;if&lt;/code&gt; and then a new method in the &lt;code&gt;Payment&lt;/code&gt; class. Another payment method comes called Coupon Payment and we are again modifying classes. Remember what does this principle says, Open for extension but close for modifications. so we are breaking this principle here.&lt;/strong&gt;&lt;br&gt;
So, how to fix it, let's apply Open Close Principle here. &lt;/p&gt;

&lt;p&gt;Let's make a new &lt;code&gt;interface&lt;/code&gt; with a method named &lt;code&gt;pay&lt;/code&gt; &lt;a href="https://www.geeksforgeeks.org/php-interface/#:~:text=PHP%20%7C%20Ternary%20Operator-,PHP%20%7C%20Interface,the%20particular%20methods%20are%20implemented.&amp;amp;text=The%20interface%20contains%20no%20data%20variables."&gt;read more&lt;/a&gt; about interfaces in PHP. &lt;br&gt;
&lt;code&gt;PayableInterface.php&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;interface PayableInterface
{
    public function pay();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Make a &lt;code&gt;class&lt;/code&gt; for each payment method and extend it with the &lt;code&gt;PayableInterface&lt;/code&gt; interface. &lt;br&gt;
&lt;code&gt;CreditCardPayment.php&lt;/code&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 CreditCardPayment implements PayableInterface
{

    public function pay()
    {
        // Implement Credit Card payment logic
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;PaypalPayment.php&lt;/code&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 PaypalPayment implements PayableInterface
{

    public function pay()
    {
        // Implement paypall payment logic
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;CreditCardPayment.php&lt;/code&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 WiretransferPayment implements PayableInterface
{

    public function pay()
    {
        // Implement Wire transfer payment logic
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, make a new class &lt;br&gt;
&lt;code&gt;PaymentFactory.php&lt;/code&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 PaymentFactory
{
    public function initializePayment($type)
    {
        if($type == 'credit'){
            // do credit card payments
            return new CreditCardPayment;
        }else if($type == 'paypal') {
            //do paypal payments
            return new PaypalPayment;
        }
        else if ($type == 'wire') {
            //do wire transfer payments
            return new WiretransferPayment;
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and finally our &lt;code&gt;pay&lt;/code&gt; method in&lt;br&gt;
&lt;code&gt;PaymentController.php&lt;/code&gt;. This pay method asks &lt;code&gt;PaymentFactory&lt;/code&gt; todo the payments and factory handle itself everything.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public function pay(Request $request)
    {
        $paymentFactory = new PaymentFactory();
        $paymentMethod = $paymentFactory-&amp;gt;initializePayment($request-&amp;gt;type);
        $paymentMethod-&amp;gt;pay();
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So what will happen next &lt;strong&gt;What if we have another payment method called Cash on Delivery&lt;/strong&gt; we will make a new &lt;code&gt;class&lt;/code&gt; called &lt;code&gt;CashOnDeliveryPayment.php&lt;/code&gt; and implement the same &lt;code&gt;payableInterface&lt;/code&gt; which will give us &lt;code&gt;pay&lt;/code&gt; method to put cash on delivery logic there. &lt;/p&gt;

&lt;p&gt;That's it. We solve the issue of modifying the classes now if the new payment method comes we extend functionality by adding new class instead of modifying the existing one. &lt;/p&gt;

&lt;p&gt;Please comment down below if you do not understand any part of the article. &lt;/p&gt;

</description>
      <category>laravel</category>
      <category>design</category>
      <category>oop</category>
      <category>solid</category>
    </item>
    <item>
      <title>Becoming a better Laravel developer by using the SOLID design principles. Part 1</title>
      <dc:creator>Abrar Ahmad</dc:creator>
      <pubDate>Fri, 17 Jul 2020 07:51:17 +0000</pubDate>
      <link>https://dev.to/abrardev99/becoming-a-better-laravel-developer-by-using-the-solid-design-principles-part-1-6f9</link>
      <guid>https://dev.to/abrardev99/becoming-a-better-laravel-developer-by-using-the-solid-design-principles-part-1-6f9</guid>
      <description>&lt;p&gt;I'm starting a series of explaining and using &lt;strong&gt;SOLID&lt;/strong&gt; Design Principales specifically in &lt;a href="//laravel.com"&gt;Laravel&lt;/a&gt;. Today, I will try to cover the first design principle &lt;strong&gt;S&lt;/strong&gt;. &lt;/p&gt;

&lt;h2&gt;
  
  
  What is SOLID
&lt;/h2&gt;

&lt;p&gt;S.O.L.I.D is an acronym for the first five object-oriented design(OOD)** principles** by Robert C. Martin, popularly known as [Uncle Bob (&lt;a href="https://en.wikipedia.org/wiki/Robert_Cecil_Martin"&gt;https://en.wikipedia.org/wiki/Robert_Cecil_Martin&lt;/a&gt;).&lt;/p&gt;

&lt;h2&gt;
  
  
  S.O.L.I.D stands for
&lt;/h2&gt;

&lt;p&gt;S - Single-responsibility principle&lt;br&gt;
O - Open-closed principle&lt;br&gt;
L - Liskov substitution principle&lt;br&gt;
I - Interface segregation principle&lt;br&gt;
D - Dependency Inversion Principle&lt;/p&gt;
&lt;h2&gt;
  
  
  Purpose of SOLID Design Principales
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;To make the code more maintainable. &lt;/li&gt;
&lt;li&gt;To make the code easier to read and understand &lt;/li&gt;
&lt;li&gt;To make it easier to quickly extend the system with new functionalities without breaking the existing ones. &lt;/li&gt;
&lt;li&gt;To make the clean code. &lt;/li&gt;
&lt;/ol&gt;
&lt;h2&gt;
  
  
  Let's look at S - Single-responsibility principle.
&lt;/h2&gt;

&lt;p&gt;According to the official definition of Single-responsibility principle&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A class should have one and only one reason to change. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;What does that mean? let's understand with an example in Laravel.&lt;br&gt;
Let's assume you have a &lt;code&gt;store&lt;/code&gt; method in the controller (for example &lt;code&gt;store&lt;/code&gt; method in &lt;code&gt;PostController&lt;/code&gt;) and &lt;code&gt;store&lt;/code&gt; method look like below with validation&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
namespace App\Http\Controllers

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use App\Post;

class PostController extends Controller
{
    public function store(Request $request)
    {

        // validate incoming request

        $validator = Validator::make($request-&amp;gt;all(), [
           'title' =&amp;gt; 'required|string|max:200',
           'description' =&amp;gt; 'required|string',
       ]);


       // finally store our post

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

&lt;/div&gt;



&lt;p&gt;let's implement the Single-responsibility principle here, Laravel provide &lt;a href="https://laravel.com/docs/6.x/validation#form-request-validation"&gt;Form Request&lt;/a&gt; object out of the box. Make Form Request using an artisan command&lt;br&gt;
&lt;br&gt;
 &lt;code&gt;php artisan make:request StoreBlogPost&lt;/code&gt;&lt;br&gt;
&lt;br&gt;
 Which will create new Request class in &lt;code&gt;app\Http\Request\StoreBlogPost&lt;/code&gt;.&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

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class StoreBlogPost extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
           'title' =&amp;gt; 'required|string|max:200',
           'description' =&amp;gt; 'required|string',
        ];
    }

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

&lt;/div&gt;



&lt;p&gt;Now change our &lt;code&gt;PostController&lt;/code&gt; to use our &lt;code&gt;StoreBlogPost&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
namespace App\Http\Controllers

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use App\Post;

class PostController extends Controller
{
    public function store(StoreBlogPost $request)
    {

       // finally store our post

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

&lt;/div&gt;



&lt;p&gt;It will automatically validate the request data and return validated requests. Now you can &lt;code&gt;$request&lt;/code&gt; the same as you use the &lt;code&gt;Request&lt;/code&gt; object. You can do a lot of other stuff with &lt;code&gt;Form Request&lt;/code&gt; like custom messages, custom validations, etc for more visit &lt;a href="https://laravel.com/docs/6.x/validation#form-request-validation"&gt;Larave official documentation&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;If you like the article please consider like and follow me, there are 4 other parts coming understanding &lt;strong&gt;SOLID&lt;/strong&gt; and using in the Laravel app. &lt;/p&gt;

&lt;p&gt;Cheers!&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>design</category>
      <category>solid</category>
    </item>
    <item>
      <title>How to use Laravel Blade @stack('scripts')</title>
      <dc:creator>Abrar Ahmad</dc:creator>
      <pubDate>Tue, 14 Jul 2020 15:08:29 +0000</pubDate>
      <link>https://dev.to/abrardev99/how-to-use-laravel-blade-stack-scripts-2752</link>
      <guid>https://dev.to/abrardev99/how-to-use-laravel-blade-stack-scripts-2752</guid>
      <description>&lt;p&gt;I see a lot of people are not getting benefits from laravel blade fully. One of blade directive &lt;code&gt;@stack('scripts')&lt;/code&gt; is very helpful when you have javascript need to execute in the child page. &lt;/p&gt;

&lt;p&gt;I create a fresh Laravel installation to demo an example. I have to make auth scaffolding with laravel/ui package because I'm using Laravel 6.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is stack('anyname') directive
&lt;/h2&gt;

&lt;p&gt;Stack directive as self-explaining it stacks the scripts where you want to stack them. &lt;/p&gt;

&lt;h2&gt;
  
  
  How to use
&lt;/h2&gt;

&lt;p&gt;Let's say you have a master view and child views. And you have some javascript operations specifically related to child page may be an Ajax request. Now it is not good practice to put every javascript on the master page. It will slow down the speed because it is executing child pages scripts that don't need. &lt;/p&gt;

&lt;p&gt;So what we need to do, in our master page just before closing body tag make &lt;code&gt;@stack&lt;/code&gt; like below code.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;!-- Scripts --&amp;gt;&lt;br&gt;
    &amp;lt;script src="{{ asset('js/app.js') }}" &amp;gt;&amp;lt;/script&amp;gt;&lt;br&gt;
    @stack('child-scripts')&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now in our child page, let's say home, you have alert specific to the child, how to do that&lt;/p&gt;

&lt;p&gt;&lt;code&gt;@extends('layouts.app')&lt;/code&gt;&lt;br&gt;
&lt;code&gt;@section('content')&lt;/code&gt;&lt;br&gt;
&lt;code&gt;&amp;lt;div&amp;gt;I'm child&amp;lt;/div&amp;gt;&lt;/code&gt;&lt;br&gt;
&lt;code&gt;@endsection&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;@push('child-scripts')&lt;/code&gt;&lt;br&gt;
    &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt;&lt;br&gt;
        &lt;code&gt;alert('I\'m coming from child')&lt;/code&gt;&lt;br&gt;
    &lt;code&gt;&amp;lt;/script&amp;gt;&lt;/code&gt;&lt;br&gt;
&lt;code&gt;@endpush&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;And alert is coming from child&lt;br&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%2Fi%2F4u82wdi98atkeq4465wm.png" 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%2Fi%2F4u82wdi98atkeq4465wm.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  My thoughts:
&lt;/h2&gt;

&lt;p&gt;It is a very clean way of writing javascript. &lt;br&gt;
It improves code readability and performance.&lt;br&gt;
And it will always go to the end of the page as recommended where you put &lt;code&gt;@stack&lt;/code&gt;, directive does not matter from where &lt;code&gt;push&lt;/code&gt; is coming.   &lt;/p&gt;

</description>
      <category>laravel</category>
      <category>blade</category>
      <category>tip</category>
    </item>
  </channel>
</rss>
