<?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: Saravana Sai</title>
    <description>The latest articles on DEV Community by Saravana Sai (@saravanasai).</description>
    <link>https://dev.to/saravanasai</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%2F852744%2Fdf7cb2b0-d261-4d8b-8658-ce864d3152be.jpeg</url>
      <title>DEV Community: Saravana Sai</title>
      <link>https://dev.to/saravanasai</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/saravanasai"/>
    <language>en</language>
    <item>
      <title>Atomic locks in laravel explained in depth</title>
      <dc:creator>Saravana Sai</dc:creator>
      <pubDate>Sun, 29 Jan 2023 16:18:11 +0000</pubDate>
      <link>https://dev.to/saravanasai/atomic-locks-in-laravel-explained-in-depth-28j8</link>
      <guid>https://dev.to/saravanasai/atomic-locks-in-laravel-explained-in-depth-28j8</guid>
      <description>&lt;h2&gt;
  
  
  What is Atomic lock?
&lt;/h2&gt;

&lt;p&gt;Atomic lock is not a term directly related to a laravel itself.The term is actually from database.I hope you have know about ACID principle in database.They are&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Atomicity&lt;/li&gt;
&lt;li&gt;Consistency&lt;/li&gt;
&lt;li&gt;Isolation&lt;/li&gt;
&lt;li&gt;Durability&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Atomic lock comes under the first property atomicity.To learn more about &lt;a href="https://www.ibm.com/docs/en/cics-ts/5.4?topic=processing-acid-properties-transactions" rel="noopener noreferrer"&gt;ACID property&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Let jump into practical usage: &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Scenario:&lt;/strong&gt;  &lt;/p&gt;

&lt;p&gt;In at some point of your dev life or else you may have built a e-commerce application or some kind of reservation system.just think of the situation in e-commerce application two user where placing the order at same time.but,the stocks left is only 1.As we know all web application process request in concurrently it may cause some in-consistency. &lt;/p&gt;

&lt;p&gt;example: 2-orders may placed but we know that only 1 left in stock.&lt;/p&gt;

&lt;p&gt;How will handle this situation? roll up your heads for some time for solution just share your idea in comments if you faced the same kind of situation and how did you handle this problem? &lt;/p&gt;

&lt;p&gt;if you are love to watch video based content for the solution then just watch the video below &amp;amp; subscribe the channel in YouTube called LaravelDaily.This article is inspired from this video content&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/zybiFatkoCo"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;For this kind of solution where the Laravel Atomic lock come into play which help us to handle this problem like pro.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F0f458nsoz5c8mj9223gj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F0f458nsoz5c8mj9223gj.png" alt="Image description" width="676" height="463"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can write application logic inside the &lt;code&gt;Cache::lock&lt;/code&gt; method &amp;amp; the that time you specified it wont process other request under the hood laravel uses database driver to implement this feature for you.Once the specified time is completed it will process the next request in a queue.&lt;/p&gt;

&lt;p&gt;So,The first order get placed after that the stock left would become 0,because we are processing the request in a queue.when a second request comes in the stock left is 0.the second order get rejected.you can use this for all kind of raced scenario also example ticket reservation &amp;amp; hotel bookings etc. &lt;/p&gt;

&lt;p&gt;Hopefully this will be useful to some of you :)&lt;/p&gt;

&lt;p&gt;I'd love to hear what you're comments &amp;amp; more example let me know in the comments below!&lt;/p&gt;

&lt;p&gt;Follow for more content like this &amp;amp; share    &lt;/p&gt;

</description>
      <category>gratitude</category>
    </item>
    <item>
      <title>Interfaces and Polymorphism in PHP - Practical Guide</title>
      <dc:creator>Saravana Sai</dc:creator>
      <pubDate>Sat, 21 Jan 2023 03:58:38 +0000</pubDate>
      <link>https://dev.to/saravanasai/interfaces-and-polymorphism-in-php-practical-guide-pfc</link>
      <guid>https://dev.to/saravanasai/interfaces-and-polymorphism-in-php-practical-guide-pfc</guid>
      <description>&lt;h2&gt;
  
  
  Polymorphism
&lt;/h2&gt;

&lt;p&gt;Polymorphism in OOP's is a concept that allows you to create classes with different functionalities in a single interface generally, it is of two types: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;compile-time (overloading) &lt;/li&gt;
&lt;li&gt;run time (overriding)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;But polymorphism in PHP does not support overloading, or in other words, compile-time polymorphism.&lt;/p&gt;

&lt;p&gt;! Let don’t have to worry about the technicalities &amp;amp; jargon's. Let see a example with a explanation.&lt;/p&gt;

&lt;h2&gt;
  
  
  Scenario:
&lt;/h2&gt;

&lt;p&gt;Let consider that we have to write a code for payment gateway integration &amp;amp; they may be more than one payment gateway (stripe &amp;amp; pay-pal) . The different users may choose different payment method &amp;amp; also in future there may be a some other payment methods also. &lt;/p&gt;

&lt;p&gt;Just take a min &amp;amp; roll up you heads for a solution . There are different ways to approach this with simple if-else condition but by using a PHP interface we can get a clean implementation.&lt;/p&gt;

&lt;p&gt;let's create an interface  &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fx8jqa93nxzhk0j66d2v1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fx8jqa93nxzhk0j66d2v1.png" alt="Image description" width="800" height="359"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The class which uses or implements PaymentCollectionIterface   en-forces the class to implement the collect method.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fwhir7dc3ob23gvsbmtck.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fwhir7dc3ob23gvsbmtck.png" alt="Image description" width="800" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fdwbns3qifs1e2byqylp8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fdwbns3qifs1e2byqylp8.png" alt="Image description" width="800" height="434"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;StripePaymentService implements PaymentCollectionIterface &lt;br&gt;
as well as for PaypalPaymentService.&lt;/p&gt;

&lt;p&gt;Don't take the implementation part of collect method so seriously that is just an example. Let's focus on a problem. Till now we have a two separate class for each payment method. &lt;/p&gt;

&lt;p&gt;Now let's create a PaymentService class which handle the payment detection logic. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F25jxyrunyfznmfh5fma1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F25jxyrunyfznmfh5fma1.png" alt="Image description" width="800" height="413"&gt;&lt;/a&gt;  &lt;/p&gt;

&lt;p&gt;In above code we have type hinted the&lt;br&gt;
&lt;code&gt;CollectAmount(PaymentCollectionIterface $collector)&lt;/code&gt; method with PaymentCollectionIterface.This is where the polymorphism come in to play.The argument passed to the &lt;code&gt;CollectAmount()&lt;/code&gt; method should the the instance of a class which implements the &lt;code&gt;PaymentCollectionIterface&lt;/code&gt; else it will throw an error. &lt;/p&gt;

&lt;p&gt;As we know that both &lt;strong&gt;StripePaymentService &amp;amp; PaypalPaymentService&lt;/strong&gt; implements PaymentCollectionIterface. So , PHP is smart enough to resolve that collect method on the class which the instance is passed. &lt;/p&gt;

&lt;p&gt;It resolves the payment method on run time.Now if user want to pay with paypal.you can invoke a paymentService class like below. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fkllm72xc1jmlmofy9q58.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fkllm72xc1jmlmofy9q58.png" alt="Image description" width="800" height="189"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;if user want to pay with stripe then you can do like this below. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fk7a1d64lgx6siutgd2hl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fk7a1d64lgx6siutgd2hl.png" alt="Image description" width="800" height="206"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;... So that's it a Interfaces and Polymorphism in PHP using in a more practical way &lt;/p&gt;

&lt;p&gt;Hopefully this will be useful to some of you :)&lt;/p&gt;

&lt;p&gt;I'd love to hear what you're comments &amp;amp; more example let me know in the comments below!&lt;/p&gt;

&lt;p&gt;Follow for more content like this &amp;amp; share   &lt;/p&gt;

</description>
      <category>welcome</category>
    </item>
    <item>
      <title>Laravel Facades - How it works under the hood</title>
      <dc:creator>Saravana Sai</dc:creator>
      <pubDate>Sat, 31 Dec 2022 21:04:57 +0000</pubDate>
      <link>https://dev.to/saravanasai/laravel-facades-how-it-works-under-the-hoods-4jk6</link>
      <guid>https://dev.to/saravanasai/laravel-facades-how-it-works-under-the-hoods-4jk6</guid>
      <description>&lt;p&gt;Hello ! PHP &amp;amp; Laravel devs.Have you ever thought how does the  &lt;a href="https://laravel.com/docs/9.x/facades"&gt;Laravel Facades&lt;/a&gt; static class call works with a method chaining. yeap I don't want to overwhelm . Let see a code example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Calculator::add(100)
    -&amp;gt;subract(50)
    -&amp;gt;add(30)
    -&amp;gt;add(10);

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

&lt;/div&gt;



&lt;p&gt;In above code you can see a static method call  &lt;strong&gt;Calculator&lt;/strong&gt; class calling make function statically then have you ever wonder how the method chaining is working in a static function call.&lt;/p&gt;

&lt;p&gt;How is this magic is happening under the hood of &lt;a href="https://github.com/saravanasai/framework/blob/9.x/src/Illuminate/Support/Facades/Facade.php"&gt;Laravel Framework&lt;/a&gt; . Let we start figure it out.&lt;/p&gt;

&lt;p&gt;In Laravel they used the simple PHP Magic Functions to build that kind of behavior. Stop What is Magic Functions in PHP &lt;a href="https://www.php.net/manual/en/language.oop5.magic.php"&gt;Magic Functions in PHP&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Definition :&lt;/strong&gt; &lt;br&gt;
Magic methods are special methods which override PHP's default's action when certain actions are performed on an object.&lt;/p&gt;

&lt;p&gt;Laravel Facade using &lt;code&gt;__call()&lt;/code&gt; &amp;amp; &lt;code&gt;__callStatic()&lt;/code&gt; to implement this feature. Before see the code let understand how this both magic method works. &lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;__call() Method:&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;__call() is triggered when invoking inaccessible methods in an object context.&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;__callStatic() Method:&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;__callStatic() is triggered when invoking inaccessible methods in a static context. &lt;/p&gt;

&lt;p&gt;I know its lot of theory.Let see the implementation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/**
 * Summary of Calculator
 * @var int $value
 * @static add()
 * @static subract()
 * @
 */
class Calculator extends hasFacade
{

    public int $value = 0;

    protected array $mappings = [
        'add' =&amp;gt; 'addtion',
        'subract' =&amp;gt; 'subtraction'
    ];

    /** 
     * Summary of add
     * @param int $no
     * @static add
     * @return Calculator
     */
    protected function addtion(int $no): self
    {

        $this-&amp;gt;value += $no;

        return $this;

    }

    /**
     * Summary of subtract
     * @param int $no
     * @return Calculator
     */
    protected function subtraction(int $no): self
    {

        $this-&amp;gt;value -= $no;

        return $this;

    }

    public function __call(string $name, array $arguments): self
    {
        $functionName = $this-&amp;gt;mappings[$name];

        return $this-&amp;gt;$functionName(...$arguments);

    }

    public static function __callStatic(string $method, array $arguments): self
    {

        $instance = (new static);

        $calledFuction = $instance-&amp;gt;mappings[$method];

        return $instance-&amp;gt;$calledFuction(...$arguments);

    }

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

&lt;/div&gt;



&lt;p&gt;You can see the implementation and now you can use this &lt;code&gt;Calculator&lt;/code&gt; class with static methods like this in below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$finalValue = Calculator::add(100)
    -&amp;gt;subract(50)
    -&amp;gt;add(30)
    -&amp;gt;add(10);

var_dump($finalValue-&amp;gt;value); // 90

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

&lt;/div&gt;



&lt;p&gt;Explanation:&lt;/p&gt;

&lt;p&gt;When the &lt;code&gt;Calculator&lt;/code&gt; class invoked statically function call the &lt;code&gt;add()&lt;/code&gt; method . The &lt;code&gt;__callStatic()&lt;/code&gt; method get invoked &amp;amp; It make a new instance of &lt;code&gt;Calculator&lt;/code&gt; class then it call the &lt;code&gt;addtion()&lt;/code&gt; method using that instance by resolving and also return that instance . Then next method &lt;code&gt;subraction()&lt;/code&gt; is invoked with that same instance . Then it call &lt;code&gt;__call()&lt;/code&gt; method get invoked then it all the magic happens out. For re-usability you can move the both method to new class and extend it with the class you want the implementation. &lt;/p&gt;

</description>
      <category>laravel</category>
      <category>php</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>PHP-8.0 Named Arguments - Explained in Depth</title>
      <dc:creator>Saravana Sai</dc:creator>
      <pubDate>Sat, 24 Dec 2022 17:43:45 +0000</pubDate>
      <link>https://dev.to/saravanasai/php-80-named-arguments-explained-in-depth-34di</link>
      <guid>https://dev.to/saravanasai/php-80-named-arguments-explained-in-depth-34di</guid>
      <description>&lt;p&gt;PHP supports passing arguments by 5 different types. They are &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Passing by reference (default type)&lt;/li&gt;
&lt;li&gt;Passing by value&lt;/li&gt;
&lt;li&gt;Default argument values&lt;/li&gt;
&lt;li&gt;Variable-length argument lists&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Named Arguments&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;First Lets Discuss. Why we need a named arguments.Let consider a example we are trying to write a function to calculate the house or home's monthly expenses . Some may have tax &amp;amp; water tax but,some may not.Let look a code with out using a named arguments.&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

function expense($food, $vatTax = 0, $electricity, $wTax = 0)
{
    return $food + $vatTax + $electricity + $wTax;
}

echo expense(20, 0 , 10, 10);  // user without vatTax

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

&lt;/div&gt;



&lt;p&gt;In above function you can see even if the user not having a vatTax we are passing the zero. we can pass that as a last argument. for example we are passing like this.so , just think what if we have some more arguments for some reason it may need &amp;amp; not need.&lt;/p&gt;

&lt;p&gt;This the case PHP-8.0 comes with &lt;strong&gt;Named Arguments&lt;/strong&gt; where you can pass the arguments by name . let rewrite the above function using Named Arguments&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

function expense($food, $vatTax = 0, $electricity, $wTax = 0)
{
    return $food + $vatTax + $electricity + $wTax;
}

//passing arguments by name

echo expense(food: 40, electricity: 50); 


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

&lt;/div&gt;



&lt;p&gt;So, using PHP named arguments we don't want to pass a default value for non used parameters.&lt;/p&gt;

&lt;p&gt;Share your comments and like for more post like this. &lt;/p&gt;

</description>
      <category>monitoring</category>
    </item>
    <item>
      <title>No : 1 - Example Of How Not To Write Code in Laravel</title>
      <dc:creator>Saravana Sai</dc:creator>
      <pubDate>Thu, 27 Oct 2022 16:38:46 +0000</pubDate>
      <link>https://dev.to/saravanasai/no-1-example-of-how-not-to-write-code-in-laravel-5f</link>
      <guid>https://dev.to/saravanasai/no-1-example-of-how-not-to-write-code-in-laravel-5f</guid>
      <description>&lt;h2&gt;
  
  
  Story Behind
&lt;/h2&gt;

&lt;p&gt;Hi Guys . I am Saravana Thiyagarajan a PHP developer with 1.5years on experience. I have built some decent level of apps in Laravel.&lt;/p&gt;

&lt;p&gt;After a Long Search joined in new organization which has a CRM &amp;amp; other small social networking apps . I have joined a Junior PHP developer &amp;amp; They assigned me a task on CRM Application And That is the reason for this post . &lt;/p&gt;

&lt;h2&gt;
  
  
  The Sample Code - 1
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        //check if the user is relational manager then show selected user data else login user data
        $this-&amp;gt;middleware('auth');
        $this-&amp;gt;middleware(function ($request, $next) {
            $this-&amp;gt;login_user_id = (auth()-&amp;gt;user()-&amp;gt;roles-&amp;gt;contains('slug', 'relational-manager') &amp;amp;&amp;amp; auth()-&amp;gt;user()-&amp;gt;selected_realtor) ? auth()-&amp;gt;user()-&amp;gt;selected_realtor-&amp;gt;realtor_id : auth()-&amp;gt;user()-&amp;gt;id;
            $this-&amp;gt;login_user = User::where('id',$this-&amp;gt;login_user_id)-&amp;gt;first();

            return $next($request);
        });
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Try to understand what is happening is constructor&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In whole application all controllers are simply copy pasted same the code was written by my Team Lead.&lt;/p&gt;

&lt;h2&gt;
  
  
  Solution - 1
&lt;/h2&gt;

&lt;p&gt;Just create a middleware &amp;amp; assign on routes file for all the enpoints &lt;/p&gt;

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



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Route::group(['middleware'=&amp;gt;'xxxx'],function()
{
    Route::get('payments',PaymentsController::class);
    Route::get('transactions',TransactionsController::class);

});

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

&lt;/div&gt;






&lt;p&gt;Lets see one another function to in that same controller.&lt;br&gt;
Plz forgive me . Just take a look at  the sample code. This how the whole project code is done.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Its just for example how not to code&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The Sample Code - 2
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/**
     * XXXXXXX transfer.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function XXXXXXXXXX_new(Request $request)
    { 
        if (!auth()-&amp;gt;user()-&amp;gt;canAccess('xxxxxxx')) {
            return response()-&amp;gt;json([
                'status' =&amp;gt; 'error',
                'message'=&amp;gt; "Permission denied"
            ]);
        }

        $customMessages = [
            'required' =&amp;gt; 'The :attribute field can not be empty',
            'array' =&amp;gt;'The :attribute field must be array'
        ];

        $validator = Validator::make($request-&amp;gt;all(),[
            'xxxxxxx' =&amp;gt;'required',
            'xxxxxx' =&amp;gt; 'nullable|string',
            'xxxxxx' =&amp;gt; 'nullable',
            'xxxxxx' =&amp;gt; 'nullable|array',
            'id' =&amp;gt; "required_if:select_all,==,false|array",
            "xxxxxxx" =&amp;gt; 'nullable',
        ],$customMessages);

        if($validator-&amp;gt;fails()){
            return response()-&amp;gt;json([
                'ststus'=&amp;gt;'error',
                'message' =&amp;gt;implode(',', $validator-&amp;gt;errors()-&amp;gt;all())
            ]);
        }

        $to = $request-&amp;gt;xxxxxxx;

        $transfer = [
            'status' =&amp;gt; false,
            'lead_data' =&amp;gt; []
        ];


        $filter_info_test = $request-&amp;gt;xxxxxxx;
        $filter_info = json_decode($filter_info_test, true);
        if($request-&amp;gt;filled('select_all')){
            if($request-&amp;gt;select_all == "true"){
                if($request-&amp;gt;uncheck){
                    if(count($request-&amp;gt;uncheck) &amp;gt; 0){
                        //remove unselected leads

                        $xxxx = Lxxxx::where(function($query){
                            if(auth()-&amp;gt;user()-&amp;gt;hasRole('brokerage')){
                                $query-&amp;gt;whereHas('bbbbbbbbb',function($query){
                                    $query-&amp;gt;whereHas('rrrrrrrr',function($qu){
                                        $qu-&amp;gt;where('sssss', 'brokerage');
                                    });
                                });
                            }
                            else{
                                $query-&amp;gt;where('owner',$this-&amp;gt;login_user_id)-&amp;gt;orWhereHas('tttttt',function($query1){
                                    $query1-&amp;gt;where('tttttt',$this-&amp;gt;login_user_id);
                                });
                            }
                        })-&amp;gt;whereNotIn('id',$request-&amp;gt;uuuuuuu);

                        if(!empty($infooo)){

                            if(array_key_exists('search', $infooo) == true){
                                $ls-&amp;gt;where(function($query)use($request, $filter_info){
                                    $query-&amp;gt;where('name', 'LIKE', '%'.$filter_info['search'] .'%');

                                });
                            }
                            if(array_key_exists('phone', $filter_info) == true){
                                $leads-&amp;gt;whereHas('nono', function($query) use($request, $filter_info) {
                                    $query-&amp;gt;where('label', $filter_info['phone']) ;
                                });
                            }

                            if(array_key_exists('email', $filter) == true){
                                $leads-&amp;gt;whereHas('email_address', function($query1) use($request, $filter_info) {
                                    $query1-&amp;gt;where('label', $filter_info['email']);
                                });
                            }

                            if(array_key_exists('source', $filter_info) == true){
                                $leads-&amp;gt;whereHas('sources',function($query) use($request, $filter_info) {
                                    $query-&amp;gt;whereIn('id',json_decode($filter_info['source']));
                                });
                            }

                            if(array_key_exists('stage', $filter_info) == true){
                                $leads-&amp;gt;whereHas('stages',function($query) use($request, $filter_info) {
                                    $query-&amp;gt;whereIn('id',json_decode($filter_info['stage']));
                                });
                            }

                            if(array_key_exists('tag', $filter_info) == true){
                                $leads-&amp;gt;whereHas('tags_leads',function($query) use($request, $filter_info) {
                                    $query-&amp;gt;whereIn('lead_tag_id',json_decode($filter_info['tg']));
                                });
                            }

                            if(array_key_exists('expert', $filter_info) == true){
                                $leads-&amp;gt;whereHas('transaction',function($query) use($request, $filter_info) {
                                    $query-&amp;gt;whereIn('to',json_decode($filter_info['expert']));
                                })-&amp;gt;orWhere(function($query1) use($request, $filter_info) {
                                    $query1-&amp;gt;doesntHave('transaction')-&amp;gt;whereIn('owner',json_decode($filter_info['expert']));
                                });
                            }
                        }



                        $leads_id = $leads-&amp;gt;pluck('id');
                    }
                    else{
                        //work with all leads

                        $leads = Lead::where(function($query){
                            if(auth()-&amp;gt;user()-&amp;gt;hasRole('brokerage')){
                                $query-&amp;gt;whereHas('brokerage_user',function($query){
                                    $query-&amp;gt;whereHas('roles',function($qu){
                                        $qu-&amp;gt;where('slug', 'brokerage');
                                    });
                                });
                            }
                            else{
                                $query-&amp;gt;where('owner',$this-&amp;gt;login_user_id)-&amp;gt;orWhereHas('transaction',function($query1){
                                    $query1-&amp;gt;where('to',$this-&amp;gt;login_user_id);
                                });
                            }
                        });

                        if(!empty($filter_info)){

                            if(array_key_exists('search', $filter_info) == true){
                                $leads-&amp;gt;where(function($query)use($request, $filter_info){
                                    $query-&amp;gt;where('name', 'LIKE', '%'.$filter_info['search'] .'%');

                                });
                            }
                            if(array_key_exists('phone', $filter_info) == true){
                                $leads-&amp;gt;whereHas('phone_number', function($query) use($request, $filter_info) {
                                    $query-&amp;gt;where('label', $filter_info['phone']) ;
                                });
                            }

                            if(array_key_exists('email', $filter_info) == true){
                                $leads-&amp;gt;whereHas('email_address', function($query1) use($request, $filter_info) {
                                    $query1-&amp;gt;where('label', $filter_info['email']);
                                });
                            }

                            if(array_key_exists('source', $filter_info) == true){
                                $leads-&amp;gt;whereHas('sources',function($query) use($request, $filter_info) {
                                    $query-&amp;gt;whereIn('id',json_decode($filter_info['source']));
                                });
                            }

                            if(array_key_exists('stage', $filter_info) == true){
                                $leads-&amp;gt;whereHas('stages',function($query) use($request, $filter_info) {
                                    $query-&amp;gt;whereIn('id',json_decode($filter_info['stage']));
                                });
                            }

                            if(array_key_exists('tag', $filter_info) == true){
                                $leads-&amp;gt;whereHas('tags_leads',function($query) use($request, $filter_info) {
                                    $query-&amp;gt;whereIn('lead_tag_id',json_decode($filter_info['tg']));
                                });
                            }

                            if(array_key_exists('expert', $filter_info) == true){
                                $leads-&amp;gt;whereHas('transaction',function($query) use($request, $filter_info) {
                                    $query-&amp;gt;whereIn('to',json_decode($filter_info['expert']));
                                })-&amp;gt;orWhere(function($query1) use($request, $filter_info) {
                                    $query1-&amp;gt;doesntHave('transaction')-&amp;gt;whereIn('owner',json_decode($filter_info['expert']));
                                });
                            }
                        }


                        $leads_id = $leads-&amp;gt;pluck('id');
                    }
                }else{
                    $leads = Lead::where(function($query){
                        if(auth()-&amp;gt;user()-&amp;gt;hasRole('brokerage')){
                            $query-&amp;gt;whereHas('brokerage_user',function($query){
                                $query-&amp;gt;whereHas('roles',function($qu){
                                    $qu-&amp;gt;where('slug', 'brokerage');
                                });
                            });
                        }
                        else{
                            $query-&amp;gt;where('owner',$this-&amp;gt;login_user_id)-&amp;gt;orWhereHas('transaction',function($query1){
                                $query1-&amp;gt;where('to',$this-&amp;gt;login_user_id);
                            });
                        }
                    });

                    if(!empty($filter_info)){

                        if(array_key_exists('search', $filter_info) == true){
                            $leads-&amp;gt;where(function($query)use($request, $filter_info){
                                $query-&amp;gt;where('name', 'LIKE', '%'.$filter_info['search'] .'%');

                            });
                        }
                        if(array_key_exists('phone', $filter_info) == true){
                            $leads-&amp;gt;whereHas('phone_number', function($query) use($request, $filter_info) {
                                $query-&amp;gt;where('label', $filter_info['phone']) ;
                            });
                        }

                        if(array_key_exists('email', $filter_info) == true){
                            $leads-&amp;gt;whereHas('email_address', function($query1) use($request, $filter_info) {
                                $query1-&amp;gt;where('label', $filter_info['email']);
                            });
                        }

                        if(array_key_exists('source', $filter_info) == true){
                            $leads-&amp;gt;whereHas('sources',function($query) use($request, $filter_info) {
                                $query-&amp;gt;whereIn('id',json_decode($filter_info['source']));
                            });
                        }

                        if(array_key_exists('stage', $filter_info) == true){
                            $leads-&amp;gt;whereHas('stages',function($query) use($request, $filter_info) {
                                $query-&amp;gt;whereIn('id',json_decode($filter_info['stage']));
                            });
                        }

                        if(array_key_exists('tag', $filter_info) == true){
                            $leads-&amp;gt;whereHas('tags_leads',function($query) use($request, $filter_info) {
                                $query-&amp;gt;whereIn('lead_tag_id',json_decode($filter_info['tg']));
                            });
                        }

                        if(array_key_exists('expert', $filter_info) == true){
                            $leads-&amp;gt;whereHas('transaction',function($query) use($request, $filter_info) {
                                $query-&amp;gt;whereIn('to',json_decode($filter_info['expert']));
                            })-&amp;gt;orWhere(function($query1) use($request, $filter_info) {
                                $query1-&amp;gt;doesntHave('transaction')-&amp;gt;whereIn('owner',json_decode($filter_info['expert']));
                            });
                        }
                    }

                    $leads_id = $leads-&amp;gt;pluck('id');
                }
            }
            else{
                //use old code
                $leads = Lead::where(function($query){
                    if(auth()-&amp;gt;user()-&amp;gt;hasRole('brokerage')){
                        $query-&amp;gt;whereHas('brokerage_user',function($query){
                            $query-&amp;gt;whereHas('roles',function($qu){
                                $qu-&amp;gt;where('slug', 'brokerage');
                            });
                        });
                    }
                    else{
                        $query-&amp;gt;where('owner',$this-&amp;gt;login_user_id)-&amp;gt;orWhereHas('transaction',function($query1){
                            $query1-&amp;gt;where('to',$this-&amp;gt;login_user_id);
                        });
                    }
                })-&amp;gt;whereIn('id',$request-&amp;gt;id);

                if(!empty($filter_info)){

                    if(array_key_exists('search', $filter_info) == true){
                        $leads-&amp;gt;where(function($query)use($request, $filter_info){
                            $query-&amp;gt;where('name', 'LIKE', '%'.$filter_info['search'] .'%');

                        });
                    }
                    if(array_key_exists('phone', $filter_info) == true){
                        $leads-&amp;gt;whereHas('phone_number', function($query) use($request, $filter_info) {
                            $query-&amp;gt;where('label', $filter_info['phone']) ;
                        });
                    }

                    if(array_key_exists('email', $filter_info) == true){
                        $leads-&amp;gt;whereHas('email_address', function($query1) use($request, $filter_info) {
                            $query1-&amp;gt;where('label', $filter_info['email']);
                        });
                    }

                    if(array_key_exists('source', $filter_info) == true){
                        $leads-&amp;gt;whereHas('sources',function($query) use($request, $filter_info) {
                            $query-&amp;gt;whereIn('id',json_decode($filter_info['source']));
                        });
                    }

                    if(array_key_exists('stage', $filter_info) == true){
                        $leads-&amp;gt;whereHas('stages',function($query) use($request, $filter_info) {
                            $query-&amp;gt;whereIn('id',json_decode($filter_info['stage']));
                        });
                    }

                    if(array_key_exists('tag', $filter_info) == true){
                        $leads-&amp;gt;whereHas('tags_leads',function($query) use($request, $filter_info) {
                            $query-&amp;gt;whereIn('lead_tag_id',json_decode($filter_info['tg']));
                        });
                    }

                    if(array_key_exists('expert', $filter_info) == true){
                        $leads-&amp;gt;whereHas('transaction',function($query) use($request, $filter_info) {
                            $query-&amp;gt;whereIn('to',json_decode($filter_info['expert']));
                        })-&amp;gt;orWhere(function($query1) use($request, $filter_info) {
                            $query1-&amp;gt;doesntHave('transaction')-&amp;gt;whereIn('owner',json_decode($filter_info['expert']));
                        });
                    }
                }

                $leads_id = $leads-&amp;gt;pluck('id');
            }
        } else{
            //use old code
            $leads = Lead::where(function($query){
                if(auth()-&amp;gt;user()-&amp;gt;hasRole('brokerage')){
                    $query-&amp;gt;whereHas('brokerage_user',function($query){
                        $query-&amp;gt;whereHas('roles',function($qu){
                            $qu-&amp;gt;where('slug', 'brokerage');
                        });
                    });
                }
                else{
                    $query-&amp;gt;where('owner',$this-&amp;gt;login_user_id)-&amp;gt;orWhereHas('transaction',function($query1){
                        $query1-&amp;gt;where('to',$this-&amp;gt;login_user_id);
                    });
                }
            })-&amp;gt;whereIn('id',$request-&amp;gt;id);

            if(!empty($filter_info)){

                if(array_key_exists('search', $filter_info) == true){
                    $leads-&amp;gt;where(function($query)use($request, $filter_info){
                        $query-&amp;gt;where('name', 'LIKE', '%'.$filter_info['search'] .'%');

                    });
                }
                if(array_key_exists('phone', $filter_info) == true){
                    $leads-&amp;gt;whereHas('phone_number', function($query) use($request, $filter_info) {
                        $query-&amp;gt;where('label', $filter_info['phone']) ;
                    });
                }

                if(array_key_exists('email', $filter_info) == true){
                    $leads-&amp;gt;whereHas('email_address', function($query1) use($request, $filter_info) {
                        $query1-&amp;gt;where('label', $filter_info['email']);
                    });
                }

                if(array_key_exists('source', $filter_info) == true){
                    $leads-&amp;gt;whereHas('sources',function($query) use($request, $filter_info) {
                        $query-&amp;gt;whereIn('id',json_decode($filter_info['source']));
                    });
                }

                if(array_key_exists('stage', $filter_info) == true){
                    $leads-&amp;gt;whereHas('stages',function($query) use($request, $filter_info) {
                        $query-&amp;gt;whereIn('id',json_decode($filter_info['stage']));
                    });
                }

                if(array_key_exists('tag', $filter_info) == true){
                    $leads-&amp;gt;whereHas('tags_leads',function($query) use($request, $filter_info) {
                        $query-&amp;gt;whereIn('lead_tag_id',json_decode($filter_info['tg']));
                    });
                }

                if(array_key_exists('expert', $filter_info) == true){
                    $leads-&amp;gt;whereHas('transaction',function($query) use($request, $filter_info) {
                        $query-&amp;gt;whereIn('to',json_decode($filter_info['expert']));
                    })-&amp;gt;orWhere(function($query1) use($request, $filter_info) {
                        $query1-&amp;gt;doesntHave('transaction')-&amp;gt;whereIn('owner',json_decode($filter_info['expert']));
                    });
                }
            }

            $leads_id = $leads-&amp;gt;pluck('id');
        }

        foreach($leads_id as $id){

            $lead = Lead::where('id',$id)-&amp;gt;first();
            if($lead){
                $from = empty($lead-&amp;gt;transaction) ? config('envs.super_user_id') : $lead-&amp;gt;transaction-&amp;gt;to;
                $leda_user = User::where('id',$lead-&amp;gt;user_id)-&amp;gt;first();
                if(empty($lead-&amp;gt;transaction)||$from != $to){

                    $lead_transfer_data = [
                        'lead_id' =&amp;gt; $id,
                        'from' =&amp;gt; $from,
                        'to' =&amp;gt; $to,
                        "assign_type" =&amp;gt; "Manual",
                        'user_id' =&amp;gt; auth()-&amp;gt;user()-&amp;gt;id,
                        "reason" =&amp;gt; $request-&amp;gt;reason ?? null
                    ];
                    LeadTransfer::create($lead_transfer_data);  

                    $user_from = User::where('id',$from)-&amp;gt;first();
                    $user_to = User::where('id',$to)-&amp;gt;first();

                    if($leda_user){
                        $leda_user-&amp;gt;update(['expert'=&amp;gt;$to]);

                        $transfer_data = [
                            'lead_id' =&amp;gt; $lead-&amp;gt;user_id,
                            'realtor_from' =&amp;gt; $from,
                            'realtor_to' =&amp;gt; $to,
                            'relation_manager_from' =&amp;gt; optional($user_from-&amp;gt;relational_manager)-&amp;gt;relational_manager_id,
                            'relation_manager_to' =&amp;gt; (!empty($user_to-&amp;gt;relational_manager) &amp;amp;&amp;amp; $user_to-&amp;gt;relational_manager-&amp;gt;permission-&amp;gt;contains('slug', 'chat-auto-join')) ? optional($user_to-&amp;gt;relational_manager)-&amp;gt;relational_manager_id : null,
                        ];

                        $this-&amp;gt;chatTransferCall($transfer_data);
                    }

                    //send notification

                    $transfer['status'] = true;
                    $transfer['lead_data'][] = $lead;

                    //add transaction note
                    $data22['lead_id'] = $id;
                    $data22['content'] = ucfirst(auth()-&amp;gt;user()-&amp;gt;name)." transferred the lead from ". $user_from-&amp;gt;name." to ".$user_to-&amp;gt;name .(!empty($request-&amp;gt;reason) ? " due to ".$request-&amp;gt;reason."." : "." );
                    $data22['user_id'] = auth()-&amp;gt;user()-&amp;gt;id;
                    $data22['type'] = "Automatic";
                    Note::create($data22);   

                }
            }
        }

        if($transfer['status']){
            if(auth()-&amp;gt;user()-&amp;gt;id != $to){
                //send notification

                $details = [
                    'body' =&amp;gt; ucfirst(auth()-&amp;gt;user()-&amp;gt;name).' transferred some leads',
                    'data' =&amp;gt; $transfer['lead_data'],
                    'description' =&amp;gt; ucfirst(auth()-&amp;gt;user()-&amp;gt;name).' transferred some leads',
                    'subject' =&amp;gt; 'Lead transfer - '.ucfirst(auth()-&amp;gt;user()-&amp;gt;name),
                    'user' =&amp;gt; $user_to,
                    'lead_id' =&amp;gt; $transfer['lead_data'][0]-&amp;gt;id,
                    'lead_name' =&amp;gt; $transfer['lead_data'][0]-&amp;gt;name,
                    'notificationFor' =&amp;gt;'Enquiry',
                    'lead_email' =&amp;gt; $transfer['lead_data'][0]-&amp;gt;email_address-&amp;gt;email,
                    'lead_phone' =&amp;gt; $transfer['lead_data'][0]-&amp;gt;phone_number-&amp;gt;number,
                    'lead_source'=&amp;gt;$transfer['lead_data'][0]-&amp;gt;sources-&amp;gt;name,
                    'lead_stage' =&amp;gt;$transfer['lead_data'][0]-&amp;gt;stages-&amp;gt;name
                ];

                Notification::send($user_to, new EnquiryNotification($details));
            }
        }


        return response()-&amp;gt;json([
            'status' =&amp;gt; 'success',
            'message' =&amp;gt; 'Leads transferred successfully'
        ]);

    }

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  For Solution
&lt;/h2&gt;




&lt;p&gt;I post it on next post according to the response for this post. &lt;/p&gt;

&lt;p&gt;just feel free to share your experience like this in comments section.&lt;/p&gt;

&lt;p&gt;Just Share your thoughts on this &amp;amp; give a Tip for me as a Junior developer what can i do in this Situation. &lt;/p&gt;




&lt;p&gt;like share &amp;amp; comment&lt;/p&gt;

</description>
      <category>php</category>
      <category>laravel</category>
      <category>codereview</category>
      <category>codequality</category>
    </item>
  </channel>
</rss>
