<?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: Paul Edward</title>
    <description>The latest articles on DEV Community by Paul Edward (@infinitypaul).</description>
    <link>https://dev.to/infinitypaul</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%2F1103883%2Fd449e983-0c50-4645-899b-89e9856d4f78.jpeg</url>
      <title>DEV Community: Paul Edward</title>
      <link>https://dev.to/infinitypaul</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/infinitypaul"/>
    <language>en</language>
    <item>
      <title>Laravel Optional Helper</title>
      <dc:creator>Paul Edward</dc:creator>
      <pubDate>Mon, 02 Dec 2024 10:50:31 +0000</pubDate>
      <link>https://dev.to/infinitypaul/laravel-optional-helper-1m4</link>
      <guid>https://dev.to/infinitypaul/laravel-optional-helper-1m4</guid>
      <description>&lt;h2&gt;
  
  
  Laravel Optional Helper
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;_# The “optional” function can accept any argument which will allow you to access properties or call methods on that object. If the given object is null, properties and methods will return null instead of causing an error&lt;br&gt;
_&lt;br&gt;
I saw a post on twitter by Taylor Otwell on how he use Laravel Optional helper&lt;/p&gt;
&lt;/blockquote&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%2Fbjxrxy7tnib305l7wkni.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%2Fbjxrxy7tnib305l7wkni.png" width="800" height="478"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;i said to myself, alot might not be aware of the helper method called Optional, so i decided to give it some voice on my blog today&lt;/p&gt;

&lt;p&gt;The optional method started with laravel 5.5 and i am going to show you how i have been using it just to clear up with my views&lt;/p&gt;

&lt;p&gt;First and foremost install laravel application starting from 5.5 upward and generate authentication scaffolding with&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;php artisan make:auth&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Register and account and sign in and as you can see the below the usual laravel auth scaffolding&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%2Fikkyy5gz2p9yc33i6l4m.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%2Fikkyy5gz2p9yc33i6l4m.png" width="800" height="252"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now head over to your command line and let’s make an Profile model with a one to one relationship with the User Model. For example a user with one profile, so i generating this along with the migration&lt;/p&gt;

&lt;p&gt;$ php artisan make:model Profile -m&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%2Fpb4k9vw17nko36nv8962.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%2Fpb4k9vw17nko36nv8962.png" width="800" height="140"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;go over to the Profile Migration in the database folder and add the following details&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;*public function *up()
{
    Schema::create('profiles', *function *(Blueprint $table) {
        $table-&amp;gt;bigIncrements('id');
        $table-&amp;gt;unsignedInteger('user_id')-&amp;gt;index();
        $table-&amp;gt;string('address');
        $table-&amp;gt;timestamps();
    });
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;once done, you can migrate using the &lt;strong&gt;php artisan migrate&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Just for the purpose of the tutorial, i will have to manually fill in the address details in the Profile table, relating it to my Id in the user table&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%2Faoaanz71dg0vxgr6hpuj.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%2Faoaanz71dg0vxgr6hpuj.png" width="800" height="225"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;less i forget, it actually skipped me, we need to add the one to one relationship in the user model (User.php)&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;*public function *profile(){
    return $this-&amp;gt;hasOne(Profile::*class*);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;so over in my home.blade.php, i want to go ahead and output my address on the dashboard&lt;/p&gt;

&lt;p&gt;outputting the user address using the auth helper function in relation to the profile actually works using this&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{{ auth()-&amp;gt;user()-&amp;gt;profile-&amp;gt;address }}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&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%2F2g968atb55ks1b1pywkt.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%2F2g968atb55ks1b1pywkt.png" width="800" height="181"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What if the user doesn’t have an address. to demonstrate this i delete the record i created in the profile table&lt;/p&gt;

&lt;p&gt;I did and i refreshed my dashboard, as you rightly guess, there is going to be an error because we are trying to get a property of a non object, because the profile relationship is returning NULL and actually that is what i got&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%2F7isc5r6zx9yo6328z8lx.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%2F7isc5r6zx9yo6328z8lx.png" width="800" height="174"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;in solving this previously, what would have been done is to use an IF statement and definately that will fix the problem&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;*@if(*auth()-&amp;gt;user()-&amp;gt;profile*)
    *{{ auth()-&amp;gt;user()-&amp;gt;profile-&amp;gt;address }}
*@endif*
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The only issue with this is wrapping this within an IF statement and just for the purpose of this tutorial and to lay the foundation and usefulness of optional helper, so we will be changing the above code&lt;/p&gt;

&lt;p&gt;so, we take what we are trying to access then wrap it inside the optional helper and that works&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{{ optional(auth()-&amp;gt;user()-&amp;gt;profile)-&amp;gt;address }}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;the optional helper will introduce a dynamic getter on whatever we passed into it and that means if the property we are trying to access doesn’t exist, it will silently return null&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%2Fbtjysevq1hlusnmu826v.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%2Fbtjysevq1hlusnmu826v.png" width="800" height="166"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;and that works as well, so let go ahead and add an address back into the Profile table to be sure everything is fine&lt;/p&gt;

&lt;p&gt;and yes it works&lt;/p&gt;

&lt;p&gt;So if you are wrapping loads of IF then the optional helper is really going to help you out and of course you can use this absolutely anywhere and not just limited to views,&lt;/p&gt;

&lt;p&gt;in conclusion you can use it to tidy up your code. we also have the Optional() callback. i will be writing about it soon in case you need it.&lt;/p&gt;

&lt;p&gt;Happy Coding&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Good Looking IF Statement</title>
      <dc:creator>Paul Edward</dc:creator>
      <pubDate>Mon, 02 Dec 2024 10:18:07 +0000</pubDate>
      <link>https://dev.to/infinitypaul/good-looking-if-statement-46nl</link>
      <guid>https://dev.to/infinitypaul/good-looking-if-statement-46nl</guid>
      <description>&lt;h2&gt;
  
  
  Good Looking IF Statement
&lt;/h2&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%2F7s3sbrbb9xbeg6m1tfy8.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%2F7s3sbrbb9xbeg6m1tfy8.png" alt="Nested IF" width="756" height="504"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;IF statement are very big part of development, the essence of the write up is to avoid deep nested if statements, although there are no fixed rules for every situation.&lt;/p&gt;

&lt;p&gt;There are couples of advise i am going to give you and when you apply this you will find out your code looks a lot cleaner , Just to start with, Let's look at a basic example&lt;/p&gt;

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



class *user
{
    *public function Location*(){
        *if*($this-&amp;gt;longitude &amp;amp;&amp;amp; $this-&amp;gt;latitude){
            return $this-&amp;gt;longitude . ' '.$this-&amp;gt;latitude;
        }

        *return null*;
    }

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

&lt;/div&gt;

&lt;p&gt;now we want to check if they have the Longitude, Latitude or either then concatenate the longitude, a space and the latitude, to avoid us repeating codes in our application…&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Generally what i found when i develop is always return what you are expecting last, do the checks you need first and then dont nest the return of the value you expect to see&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;what do i mean?&lt;/p&gt;

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



class *user
{
   *public function *Location(){
        *if*(!$this-&amp;gt;longitude || !$this-&amp;gt;latitude){
            *return null*;
        }
        //Other checks can come here
        *return *$this-&amp;gt;longitude . ' '.$this-&amp;gt;latitude;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The way i look at this is perhaps, we are doing other checks, if we are to do it the first way and we have to return another checks that means we may have to duplicate codes, so i add the expected return value last and i can do as many checks as i want returning NULL, throwing Exceptions or whatever you need it to do. I hope you get the idea?&lt;/p&gt;

&lt;p&gt;Now Let's look at a slightly more complex examples and this involve using Nesting IF and also using IF ELSE, see what i meant below&lt;/p&gt;

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

if *() {
    *if*(){
        *if *(){

        }
    }
} *else *{

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

&lt;/div&gt;

&lt;p&gt;if you look at the above, this could be much pain to bear when you trying to debug or trying to go back to change things and tend to get really messy too quickly, i have been there in the pass and i guess you too.&lt;/p&gt;

&lt;p&gt;Now let's look at an example&lt;/p&gt;

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

*if*(*ISSET*($_POST['file'])) {
    *if*(*in_array*($fileExtension, $allowExtention)) {
        *//Upload Files
    *} *else *{
     //Another Set Of Errors

    }
} *else *{
    *//display errrors
    return*;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;now am going to tell you, you can do all of the thing you have done above without using else and without nesting.. now lets tidy this up&lt;/p&gt;

&lt;p&gt;Firstly &lt;em&gt;Think About Things That Are Negative&lt;/em&gt;&lt;/p&gt;

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

*if*(!*isset*($_POST['file'])){
    *return*;
    *//Error here
*}
*//At This Point You are breaking the flow of the application here and you might be redirecting the user here

if*(*in_array*($fileExtension, $allowExtention)){
    *return *;
    *//Checking for Faulty Value
*}

*//Upload The file, and write other codes*
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;If you think about it this way, you will end up with a much leaner and cleaner code. Easy to read, Easy to Maintain&lt;/p&gt;

&lt;p&gt;Lastly, Improving code readability is an art, not a science. Sometimes it’s best to avoid negations in if statements, and sometimes it’s best to handle the most common case first and deal with errors later. But it’s always best to avoid nesting logic too deeply.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Taming the Beast of Intermittent Failures: A Laravel Circuit Breaker Tale</title>
      <dc:creator>Paul Edward</dc:creator>
      <pubDate>Mon, 02 Dec 2024 10:13:09 +0000</pubDate>
      <link>https://dev.to/infinitypaul/taming-the-beast-of-intermittent-failures-a-laravel-circuit-breaker-tale-4h87</link>
      <guid>https://dev.to/infinitypaul/taming-the-beast-of-intermittent-failures-a-laravel-circuit-breaker-tale-4h87</guid>
      <description>&lt;h2&gt;
  
  
  Taming the Beast of Intermittent Failures: A Laravel Circuit Breaker Tale
&lt;/h2&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%2Fcdn-images-1.medium.com%2Fmax%2F8064%2F0%2AoOW5fQ96M-coTKdf" 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%2Fcdn-images-1.medium.com%2Fmax%2F8064%2F0%2AoOW5fQ96M-coTKdf" alt="Photo by [Troy Bridges](https://unsplash.com/ja/@esptroy?utm_source=medium&amp;amp;utm_medium=referral) on [Unsplash](https://unsplash.com?utm_source=medium&amp;amp;utm_medium=referral)" width="800" height="600"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Ring, ring! The alarm bells are going off again. The once reliable third-party service, our good old friend (Trading Service), has started acting out like a rebellious teenager. Sigh. Any seasoned developer would get a few more grey hairs from inconsistent answers and sporadic failures.&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%2Fatl9f7xeij5p5kk1qrox.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%2Fatl9f7xeij5p5kk1qrox.png" width="800" height="800"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Well, let’s not give up, ladies and gentlemen. This is not my first time at the game, and I’m here to share a small Laravel magic trick that has more than once kept me sane. Let’s talk about implementing a Circuit Breaker when everyone is together.&lt;/p&gt;

&lt;p&gt;A Circuit Breaker? you ask. Are we going to dabble in some electrical engineering? Laughs in code. Not quite! A circuit breaker is a design pattern that stops a system from continually attempting to carry out an operation that is likely to fail.&lt;/p&gt;

&lt;p&gt;Now, let’s get our hands dirty with some code.&lt;/p&gt;

&lt;p&gt;Here is some Laravel code that illustrates how to remain resilient in the face of resistance from others:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public function handle(CompleteKyc $event)
{
    try {
        $delay = $this-&amp;gt;applyCircuitBreaker();
        if ($delay !== null) {
            $this-&amp;gt;release($delay);
        }
        $result = $this-&amp;gt;createZanibalAccount($event);

        Cache::forget('failures');
        Cache::forget('circuit:open');
    } catch (Exception $exception) {
        if($exception-&amp;gt;getCode() &amp;gt;= 500){
          $this-&amp;gt;handleException($exception);
          $this-&amp;gt;release(600);
        }

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

&lt;/div&gt;

&lt;p&gt;You’ll see that this Laravel job/listener has a fair dosage of self-preservation ingrained in them.&lt;/p&gt;

&lt;p&gt;The major performance is provided by the handle method. Using our helpful neighbor $this-&amp;gt;applyCircuitBreaker(), it determines whether a circuit is open (signifying a recent failure). If the circuit is indeed open, it hibernates like a well-fed bear, delaying any future demands until the situation is under control and the coast is clear.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;protected function applyCircuitBreaker()
{
    $lastFailureTimestamp = Cache::get('circuit:open');
    if (is_int($lastFailureTimestamp) &amp;amp;&amp;amp; time() - $lastFailureTimestamp &amp;lt; 8 * 60) {
        return $lastFailureTimestamp + 600 + rand(1, 120);
    } else {
        $this-&amp;gt;halfOpen = true;
        return null;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In the &lt;strong&gt;applyCircuitBreaker method&lt;/strong&gt;, the magic happens. It obtains the date of the most recent failure and, if it is older than eight minutes, requires a delay before the following request. For a touch of unpredictability, a random additional delay (between 1 and 120 seconds) is introduced. Variety is the flavor of life, after all, and even code needs to be jazzed up occasionally!&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;protected function handleException(Exception $exception)
{
    if ($this-&amp;gt;halfOpen) {
        Cache::put('circuit:open', time(), 600);
        return $this-&amp;gt;release(600);
    }
    if (!$failures = Cache::get('failures')) {
        Cache::put('failures', 1, 60);
    } else {
        Cache::increment('failures');
    }

    if (Cache::get('failures') &amp;gt; 10) {
        Cache::put('circuit:open', time(), 600);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Should an exception occur (and let’s face it, exceptions happen to the best of us), the &lt;strong&gt;handleException&lt;/strong&gt; method swoops into action like a superhero in a cape. It increments a failure count in our cache, but after ten failures, it drops the big bomb and opens the circuit, preventing further heartache (or in this case, request-ache) for a cooling-off period.&lt;/p&gt;

&lt;p&gt;Like any good drama, there’s a plot twist. If the circuit is in a ‘half-open’ state — meaning it’s testing the waters after a previous failure — and an exception occurs, it immediately halts the operation and reopens the circuit.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;protected function createZanibalAccount(CompleteKyc $event){
    //Your third party API
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In the end, this code does more than just carry out duties; it also tells a story about overcoming difficulty, rising above failure, and making sure that our system can withstand the unpredictability of using third-party services. It recognizes that sometimes you have to take a step back, gather your breath, and only then go back into the fight, much like a stubborn boxer in the ring.&lt;/p&gt;

&lt;p&gt;So the next time your third-party service decides to throw a tantrum, don’t panic! Just pull out your reliable circuit breaker, and let it do the heavy lifting. Just remember, as I always say, “Keep calm, code on!”&lt;/p&gt;

&lt;p&gt;That’s it for now. Until next time, may your code be bug-free and your coffee strong.&lt;/p&gt;

&lt;p&gt;Happy coding, my fellow Laravel enthusiasts!&lt;/p&gt;

</description>
    </item>
    <item>
      <title>PHP and Multithreading: A Lighthearted Look at Concurrency</title>
      <dc:creator>Paul Edward</dc:creator>
      <pubDate>Mon, 02 Dec 2024 10:12:19 +0000</pubDate>
      <link>https://dev.to/infinitypaul/php-and-multithreading-a-lighthearted-look-at-concurrency-4foa</link>
      <guid>https://dev.to/infinitypaul/php-and-multithreading-a-lighthearted-look-at-concurrency-4foa</guid>
      <description>&lt;h2&gt;
  
  
  PHP and Multithreading: A Lighthearted Look at Concurrency
&lt;/h2&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%2Fcdn-images-1.medium.com%2Fmax%2F10510%2F0%2ArJFsf_aULTP279ox" 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%2Fcdn-images-1.medium.com%2Fmax%2F10510%2F0%2ArJFsf_aULTP279ox" alt="Photo by [amirali mirhashemian](https://unsplash.com/@amir_v_ali?utm_source=medium&amp;amp;utm_medium=referral) on [Unsplash](https://unsplash.com?utm_source=medium&amp;amp;utm_medium=referral)" width="800" height="533"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Have you ever found yourself daydreaming about PHP and multithreading? Well, maybe not. But if you’re curious about how PHP handles concurrency, you’re in for a treat!&lt;/p&gt;

&lt;p&gt;Multithreading is when you have parts in the same program or task doing things in parallel. PHP and multithreading go together like, well, oil and water. While PHP does have a PTHREAD extension (though it’s currently not maintained and it’s advised to use the parallel extension instead), it only supports multithreading at the command line level. But don’t worry my friend; we won’t dive into the depths of PTHREAD today.&lt;/p&gt;

&lt;p&gt;You see, people often crave multithreading because they want to squeeze every ounce of power from their machine’s cores. But, PHP already does a fantastic job of keeping your cores busy by handling concurrent requests through web servers like PHP-FPM and there is no issue of not making use of all the cores in a machine like a standalone daemon or a CLI. So, you might say, PHP is already a multitasking powerhouse!&lt;/p&gt;

&lt;p&gt;When it comes to PHP running on your frontend web server, you should treat long-running tasks like a hot potato. Toss them to a different set of servers using a message queue or job worker setup. That way, you can asynchronously manage jobs without clogging up your front-end server. Keep those frontend servers free for, well, serving the front end!&lt;/p&gt;

&lt;p&gt;Think of it like a well-orchestrated ballet — your frontend server should dance gracefully with incoming requests and swiftly send responses back. But those long-running tasks? They belong in the background, like stagehands pulling the ropes.&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%2Fcdn-images-1.medium.com%2Fmax%2F8000%2F0%2ASJdYmD6poKSYYtcc" 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%2Fcdn-images-1.medium.com%2Fmax%2F8000%2F0%2ASJdYmD6poKSYYtcc" alt="Photo by [Marcin Simonides](https://unsplash.com/es/@cinusek?utm_source=medium&amp;amp;utm_medium=referral) on [Unsplash](https://unsplash.com?utm_source=medium&amp;amp;utm_medium=referral)" width="800" height="531"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Just as you wouldn’t run your database on your frontend web service (I’m looking at you, daredevils), you should have a dedicated database server. This way, you can optimize hardware for specific tasks, such as providing a database server with ample memory.&lt;/p&gt;

&lt;p&gt;Consider this tale of an e-commerce platform: a seemingly unified web application, but in reality, a modular masterpiece. Each aspect — product listings, cart management, and order checking — was hosted on separate server clusters. This modular approach allowed for optimal performance and scalability.&lt;/p&gt;

&lt;p&gt;Now, I know what you’re thinking. “If PHP had multithreading, we could run everything on the frontend server, right?” Hold your horses, partner! That kind of setup just wouldn’t scale. PHP’s stateless, sandboxed design makes it a horizontally scalable champion. Threading would only encourage running background tasks on front-end servers, leading to chaos and anarchy in the world of web applications.&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%2Fcdn-images-1.medium.com%2Fmax%2F16384%2F0%2AVdaCBf9QtL8ypEka" 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%2Fcdn-images-1.medium.com%2Fmax%2F16384%2F0%2AVdaCBf9QtL8ypEka" alt="Photo by [engin akyurt](https://unsplash.com/@enginakyurt?utm_source=medium&amp;amp;utm_medium=referral) on [Unsplash](https://unsplash.com?utm_source=medium&amp;amp;utm_medium=referral)" width="800" height="533"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In recent years, PHP has seen significant advancements with tools like React PHP and Swoole, which allow for asynchronous, non-blocking I/O operations in PHP. While these solutions provide new ways to handle concurrency in PHP, they are not a replacement for proper architecture when building web applications&lt;/p&gt;

&lt;p&gt;With the arrival of PHP 8, we’ve got some new kids on the block: PHP Parallel and Fibers. PHP Parallel provides a new way to achieve concurrency using parallelism, while Fibers introduce cooperative multitasking, allowing you to write asynchronous code in a more straightforward, synchronous-looking way. Although these features are exciting and open up new possibilities, they should not encourage poor architectural choices or promote running heavy background tasks on front-end web servers.&lt;/p&gt;

&lt;p&gt;While these tools can offer improved performance and concurrency, it’s essential not to lose sight of PHP’s core strengths and the importance of proper architecture in web applications. Remember to keep things modular and scalable, and your web application will sing like a well-orchestrated symphony.&lt;/p&gt;

&lt;p&gt;In summary, multithreading in PHP isn’t the holy grail of performance. Instead, focus on building modular web applications that take advantage of PHP’s concurrency capabilities. With the right architecture, PHP can be the superhero of scalability that it was always meant to be.&lt;/p&gt;

&lt;p&gt;So, the next time you find yourself pondering PHP and multithreading, remember this lighthearted tale and embrace the beauty of PHP’s concurrency capabilities. Happy coding!&lt;/p&gt;

</description>
    </item>
    <item>
      <title>The Solid Design Principle — Single Responsibility</title>
      <dc:creator>Paul Edward</dc:creator>
      <pubDate>Mon, 02 Dec 2024 10:07:04 +0000</pubDate>
      <link>https://dev.to/infinitypaul/the-solid-design-principle-single-responsibility-e67</link>
      <guid>https://dev.to/infinitypaul/the-solid-design-principle-single-responsibility-e67</guid>
      <description>&lt;h2&gt;
  
  
  The Solid Design Principle — Single Responsibility
&lt;/h2&gt;

&lt;p&gt;In this series, we’re going to discuss the five Solid Principle, this is actually applied to all Object-Oriented Programming, not just PHP&lt;/p&gt;

&lt;p&gt;The five solid designs are listed below&lt;/p&gt;

&lt;p&gt;S — Single Responsibility&lt;/p&gt;

&lt;p&gt;O — Open/Close&lt;/p&gt;

&lt;p&gt;L — Liskov Substitution&lt;/p&gt;

&lt;p&gt;I — Interface Segregation&lt;/p&gt;

&lt;p&gt;D — Dependency Inversion&lt;/p&gt;

&lt;p&gt;So in this write up I will be writing about the S&lt;/p&gt;

&lt;p&gt;S — Single Responsibility&lt;/p&gt;

&lt;p&gt;This state that a class should have only one reason to change, in simpler terms, a class should only have one task or one responsibility so if there are any other methods in a class that deals with something completely different, that method should be extracted and put somewhere else and your class should have one task/job/responsibilities&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%2F9xjoyrefhr0mjatdg752.gif" 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%2F9xjoyrefhr0mjatdg752.gif" width="500" height="279"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Why The Need For Single Responsibility&lt;/p&gt;

&lt;p&gt;if we keep our classes just with one responsibility, it does the following&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Makes the class easier to maintain.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Potentially make class reusable ( depending on whatever your class is dealing with)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Easier to test&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;

The point of the User Class above is to save a record to the database and if something goes wrong, an error message is been logged to the file and here are the methods Create, Edit and logError&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now the above class breaks the single responsibility principle because it has more than one responsibility or task, and the reason for this is because of the method logError. Create and Edit Method has perfect reasons to stay in the User Class, it actually performs actions upon the User but the logError does otherwise&lt;/p&gt;

&lt;p&gt;So how do we fix this?&lt;/p&gt;

&lt;p&gt;We can extract the logError method and keep it in its own class,&lt;/p&gt;

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

class *Logger {
    *public function *writeToFile($message){
        *//Write To File
    *}
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;we can also have flexible and expandable enough for it to save to file or save to external resource so if we still need the LogError method then use &lt;a href="https://medium.com/@infinitypaul/dependency-injection-in-php-c69109d52636" rel="noopener noreferrer"&gt;dependency injection&lt;/a&gt; to inject it into the User Class&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
Thanks for reading, I hope this clears any questions on responsibility principle, if you have any question, simply drop a comment below&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Laravel Query Builder Security</title>
      <dc:creator>Paul Edward</dc:creator>
      <pubDate>Mon, 02 Dec 2024 10:05:21 +0000</pubDate>
      <link>https://dev.to/infinitypaul/laravel-query-builder-security-3me5</link>
      <guid>https://dev.to/infinitypaul/laravel-query-builder-security-3me5</guid>
      <description>&lt;h2&gt;
  
  
  Laravel Query Builder Security
&lt;/h2&gt;

&lt;p&gt;“Never Trust A User Input” as it is widely said&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%2Fzyjs61f1wluw15iut1ue.jpeg" 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%2Fzyjs61f1wluw15iut1ue.jpeg" alt="SQL Injection" width="788" height="433"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Most people ain’t aware that PDO which is what most modern PHP application use to talk to databases, don’t have a built in mechanism for escaping column names like they do the values.&lt;/p&gt;

&lt;p&gt;for instance when you write the code below&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;User::*where*('firstname', 'infinitypaul')-&amp;gt;first();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;the ‘infinitypaul’ value is properly escaped and you safely pass user input into that of course, but the column name “firstname” there is no built in mechanism to escape the column name in PDO,so you never let your user dictate the column of your query without you verifying/sanitizing it first otherwise it is possible to have a SQL injection vulnerability in your application&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;*public function *welcome(Request $request){
    User::*where*($request-&amp;gt;firstname, 'infinitypaul')-&amp;gt;orderBy($request-&amp;gt;col)-&amp;gt;first();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;if a user enters something like this 1*&lt;em&gt;+ORDER+BY+10 **or *&lt;/em&gt;-&amp;gt;”%27))%23injectedSQL , &lt;strong&gt;or **&lt;/strong&gt;someone with ill intentions can add any malicious code they want there. the input might not make sense to you but Laravel will convert the input to an SQL query which might return an output that might give an hint to something on your database..&lt;/p&gt;

&lt;p&gt;i guess you now asking what if i need the user to dictate the column name, wait, am almost there, the best practice as at the time of writing is whitelist on the server of allowed column check just to make sure what the user is sending across matches your whitelisted set of column names, that way you know you can only pass those values&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;*public function *welcome(Request $request){
    $column_name = *array*('firstname', 'lastname', 'username');
    *if *(!*in_array*($request-&amp;gt;firstname, $column_name, true)){
        *//you can add whatever result here if not found
       return*;
    }
    User::*where*($request-&amp;gt;firstname, 'infinitypaul')-&amp;gt;first();

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

&lt;/div&gt;

&lt;p&gt;the inarray keyword checks if what the user sent is in your whitelisted value in the array. and the true makes sure it is case sensitive&lt;/p&gt;

&lt;p&gt;As short as the above will solve and prevent the database from being compromise, there are several ways in which you can prevent that, it depends on your use case.. easy as that right.. the most important factor is bringing this info to your knowledge&lt;/p&gt;

&lt;p&gt;One of the thing i love about php/laravel is the up and running community .Laravel Stated it in red in their documentation just to make you are aint ignorant of the above info&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%2Fcdn-images-1.medium.com%2Fmax%2F2440%2F1%2AZLk4o2Up2J-xm-pF6X-sMQ.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%2Fcdn-images-1.medium.com%2Fmax%2F2440%2F1%2AZLk4o2Up2J-xm-pF6X-sMQ.png" alt="Query Builder Security [https://laravel.com/docs/master/queries#introduction](https://laravel.com/docs/master/queries#introduction)" width="800" height="170"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;On a final note, you should either not allow your user control the column name at all or if you need to allow that, then you need to use a whitelist, cause if you fall vulnerable of that. it could be very disastrous,&lt;/p&gt;

&lt;p&gt;Check your apps if you know you allowed user to control the column name and correct where necessary&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Laravel Optional Helper</title>
      <dc:creator>Paul Edward</dc:creator>
      <pubDate>Mon, 02 Dec 2024 10:03:36 +0000</pubDate>
      <link>https://dev.to/infinitypaul/laravel-optional-helper-3b93</link>
      <guid>https://dev.to/infinitypaul/laravel-optional-helper-3b93</guid>
      <description>&lt;h2&gt;
  
  
  Laravel Optional Helper
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;h1&gt;
  
  
  &lt;em&gt;The “optional” function can accept any argument which will allow you to access properties or call methods on that object. If the given object is null, properties and methods will return null instead of causing an error&lt;/em&gt;
&lt;/h1&gt;
&lt;/blockquote&gt;

&lt;p&gt;I saw a post on twitter by Taylor Otwell on how he use Laravel Optional helper&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%2Fbjxrxy7tnib305l7wkni.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%2Fbjxrxy7tnib305l7wkni.png" width="800" height="478"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;i said to myself, alot might not be aware of the helper method called Optional, so i decided to give it some voice on my blog today&lt;/p&gt;

&lt;p&gt;The optional method started with laravel 5.5 and i am going to show you how i have been using it just to clear up with my views&lt;/p&gt;

&lt;p&gt;First and foremost install laravel application starting from 5.5 upward and generate authentication scaffolding with&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;php artisan make:auth&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Register and account and sign in and as you can see the below the usual laravel auth scaffolding&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%2Fikkyy5gz2p9yc33i6l4m.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%2Fikkyy5gz2p9yc33i6l4m.png" width="800" height="252"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now head over to your command line and let’s make an Profile model with a one to one relationship with the User Model. For example a user with one profile, so i generating this along with the migration&lt;/p&gt;

&lt;p&gt;$ php artisan make:model Profile -m&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%2Fpb4k9vw17nko36nv8962.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%2Fpb4k9vw17nko36nv8962.png" width="800" height="140"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;go over to the Profile Migration in the database folder and add the following details&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;*public function *up()
{
    Schema::create('profiles', *function *(Blueprint $table) {
        $table-&amp;gt;bigIncrements('id');
        $table-&amp;gt;unsignedInteger('user_id')-&amp;gt;index();
        $table-&amp;gt;string('address');
        $table-&amp;gt;timestamps();
    });
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;once done, you can migrate using the &lt;strong&gt;php artisan migrate&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Just for the purpose of the tutorial, i will have to manually fill in the address details in the Profile table, relating it to my Id in the user table&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%2Faoaanz71dg0vxgr6hpuj.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%2Faoaanz71dg0vxgr6hpuj.png" width="800" height="225"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;less i forget, it actually skipped me, we need to add the one to one relationship in the user model (User.php)&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;*public function *profile(){
    return $this-&amp;gt;hasOne(Profile::*class*);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;so over in my home.blade.php, i want to go ahead and output my address on the dashboard&lt;/p&gt;

&lt;p&gt;outputting the user address using the auth helper function in relation to the profile actually works using this&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{{ auth()-&amp;gt;user()-&amp;gt;profile-&amp;gt;address }}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&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%2F2g968atb55ks1b1pywkt.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%2F2g968atb55ks1b1pywkt.png" width="800" height="181"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What if the user doesn’t have an address. to demonstrate this i delete the record i created in the profile table&lt;/p&gt;

&lt;p&gt;I did and i refreshed my dashboard, as you rightly guess, there is going to be an error because we are trying to get a property of a non object, because the profile relationship is returning NULL and actually that is what i got&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%2F7isc5r6zx9yo6328z8lx.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%2F7isc5r6zx9yo6328z8lx.png" width="800" height="174"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;in solving this previously, what would have been done is to use an IF statement and definately that will fix the problem&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;*@if(*auth()-&amp;gt;user()-&amp;gt;profile*)
    *{{ auth()-&amp;gt;user()-&amp;gt;profile-&amp;gt;address }}
*@endif*
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The only issue with this is wrapping this within an IF statement and just for the purpose of this tutorial and to lay the foundation and usefulness of optional helper, so we will be changing the above code&lt;/p&gt;

&lt;p&gt;so, we take what we are trying to access then wrap it inside the optional helper and that works&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{{ optional(auth()-&amp;gt;user()-&amp;gt;profile)-&amp;gt;address }}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;the optional helper will introduce a dynamic getter on whatever we passed into it and that means if the property we are trying to access doesn’t exist, it will silently return null&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%2Fbtjysevq1hlusnmu826v.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%2Fbtjysevq1hlusnmu826v.png" width="800" height="166"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;and that works as well, so let go ahead and add an address back into the Profile table to be sure everything is fine&lt;/p&gt;

&lt;p&gt;and yes it works&lt;/p&gt;

&lt;p&gt;So if you are wrapping loads of IF then the optional helper is really going to help you out and of course you can use this absolutely anywhere and not just limited to views,&lt;/p&gt;

&lt;p&gt;in conclusion you can use it to tidy up your code. we also have the Optional() callback. i will be writing about it soon in case you need it.&lt;/p&gt;

&lt;p&gt;Happy Coding&lt;/p&gt;

</description>
    </item>
    <item>
      <title>inder — HTML Link &lt;a&gt; Download Attribute</title>
      <dc:creator>Paul Edward</dc:creator>
      <pubDate>Mon, 02 Dec 2024 10:02:26 +0000</pubDate>
      <link>https://dev.to/infinitypaul/inder-html-link-download-attribute-4b2e</link>
      <guid>https://dev.to/infinitypaul/inder-html-link-download-attribute-4b2e</guid>
      <description>&lt;p&gt;Reminder — HTML Link &lt;a&gt; Download Attribute&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%2Fip42bd504u0vgznsbcdo.jpeg" 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%2Fip42bd504u0vgznsbcdo.jpeg" width="800" height="474"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;“download” is a thing on &lt;a&gt; tags, download the file when clicking on a link instead of navigating to the file&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="%E2%80%9D/images/myimage.jpg%E2%80%9D"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You could even name the file to be downloaded by giving the download attribute a value.&lt;/p&gt;

&lt;p&gt;&lt;a href="%E2%80%9D/images/infinitypaul.jpg%E2%80%9D"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The download attribute is actually new for HTML 5&lt;/p&gt;

&lt;p&gt;There are no limit to the value you can allow, you can try it with the following extensions — .img .pdf .txt and so on&lt;/p&gt;

&lt;p&gt;Although naming the file only works if it’s being downloaded from the same domain as the current page. Otherwise it uses the actual filename or a browser default if no name can be determined that is If the value is omitted, the original filename is used.&lt;/p&gt;

&lt;p&gt;On a final note, I actually wish more browsers supports the download attributes , but you you can give it a short on the following browser version&lt;/p&gt;

&lt;p&gt;Chrome: 14.0&lt;/p&gt;

&lt;p&gt;Internet Explorer; 13.0&lt;/p&gt;

&lt;p&gt;Mozilla: 20.0&lt;/p&gt;

&lt;p&gt;Safari: 10.0&lt;/p&gt;

&lt;p&gt;Opera: 15.0&lt;/p&gt;

&lt;p&gt;This are the ones I know for now if there is an update or you have tried it on other version of indicate in the comment section&lt;/p&gt;

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

</description>
    </item>
    <item>
      <title>Dependency Injection in PHP</title>
      <dc:creator>Paul Edward</dc:creator>
      <pubDate>Mon, 02 Dec 2024 10:01:32 +0000</pubDate>
      <link>https://dev.to/infinitypaul/dependency-injection-in-php-1bpd</link>
      <guid>https://dev.to/infinitypaul/dependency-injection-in-php-1bpd</guid>
      <description>&lt;h2&gt;
  
  
  Dependency Injection in PHP
&lt;/h2&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%2Fwt1rmb8pgcdbabycwfyp.jpeg" 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%2Fwt1rmb8pgcdbabycwfyp.jpeg" width="760" height="535"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The main advantages of using dependency injection is that it encourages modular programming (The process of subdividing a computer &lt;strong&gt;program&lt;/strong&gt; into separate sub-programs)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The first thing we need to talk about is what dependencies are. So in dependency injection, dependencies are objects that you need to use in your class and injection simply means passing those dependencies into your class, simply put, a dependency is just another object that your class needs to function&lt;/p&gt;

&lt;p&gt;There are different ways of injecting dependencies into your classes but the most common way is through the constructor of your class.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;*class *UserRepository
{
    *private *$db;
    *private *$config;
    *private *$user;

    *public function *__construct(Database $db, Config $config, User $user)
    {

        $this-&amp;gt;db = $db;
        $this-&amp;gt;config = $config;
        $this-&amp;gt;user = $user;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Above is my constructor method, which will bring the dependencies into UserRepository as class properties, so I can use them throughout the class. Rather than instantiating them within my class, I’ll inject them when UserRepository is itself instantiated.&lt;/p&gt;

&lt;p&gt;In conjunction with interfaces — you can treat classes as “black boxes” where you can change the dependencies and know that nothing will break!&lt;/p&gt;

&lt;p&gt;This seems great right? your code will start to feel more like Lego and less like spaghetti. There are many other benefits too.&lt;/p&gt;

&lt;p&gt;Testing becomes easier,&lt;/p&gt;

&lt;p&gt;Refactoring becomes easier and&lt;/p&gt;

&lt;p&gt;Reusing code is easier.&lt;/p&gt;

&lt;p&gt;Life for a coder who uses dependency injection is far easier.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
For me, personally, dependency injection has improved my code quality and it is certainly worth implementing.&lt;/p&gt;

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

</description>
    </item>
    <item>
      <title>Is PHP Dead On Windows? Rate Of Impact?</title>
      <dc:creator>Paul Edward</dc:creator>
      <pubDate>Mon, 02 Dec 2024 09:58:31 +0000</pubDate>
      <link>https://dev.to/infinitypaul/is-php-dead-on-windows-rate-of-impact-1f90</link>
      <guid>https://dev.to/infinitypaul/is-php-dead-on-windows-rate-of-impact-1f90</guid>
      <description>&lt;h2&gt;
  
  
  Is PHP Dead On Windows? Rate Of Impact?
&lt;/h2&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%2Firifuwue34ukfwks1k52.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%2Firifuwue34ukfwks1k52.png" width="800" height="558"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;We know that the current cadence is 2 years from release for bug fixes, and 1 year after that for security fixes. This means that PHP 7.2 will be going out of support in November. PHP 7.3 will be going into security fix mode only in November. PHP 7.4 will continue to have another year of bug fix and then one year of security fixes. We are committed to maintaining development and building of PHP on Windows for 7.2, 7.3 and 7.4 as long as they are officially supported. We are not, however, going to be supporting PHP for Windows in any capacity for version 8.0 and beyond.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Microsoft recently announced PHP wouldn’t be supported on Windows, so should I be concerned if I’m a PHP developer? Does this mean the end of PHP?&lt;/p&gt;

&lt;p&gt;If you are an experienced PHP developer, I know you ain’t bothered or concerned of the recent announcement, because 99% of the time, if you are deploying your PHP application, you are going to be implementing it on a Linux environment and not on a Microsoft server. I’m pretty sure most PHP developers are not even aware of that fact that Microsoft support PHP, so I believe the announcement is a non-issue.&lt;/p&gt;

&lt;p&gt;All I can deduce from the announcement is there are probably not too many people using PHP on Windows in the first place.&lt;/p&gt;

&lt;p&gt;You have to understand Microsoft has a very open dispositional vis-a-vis programming language. In the old days, Microsoft was very centric towards Microsoft technologies and products, but these days they’ve opened up and realized that they can embrace different frameworks, languages and operating systems. So currently you can install a Linux operating system on windows pretty quickly, and that is because Microsoft is making that happen.&lt;/p&gt;

&lt;p&gt;If you need to run PHP, you are likely going to be developing a PHP application in the environment of deployment, which in most cases is on a Linux server&lt;/p&gt;

&lt;p&gt;On a windows environment, you can install Linux and then run your PHP application on that Linux environment all within your Windows operating system and voila, problem solved or probably just run PHP on a Docker environment&lt;/p&gt;

&lt;p&gt;If you look at the latest PHP usage statistics depending on which one you are looking at, PHP is typically in the top 5 or 6 most popular widely-used programming language in the world. 30 to 35 per cent of websites run PHP in some form or another, so just because Microsoft won’t support PHP anymore doesn’t mean some other company won’t step up to do it, having in mind that PHP is open-sourced.&lt;/p&gt;

&lt;p&gt;Lastly, if there is going be a severe need for PHP on windows, you can be sure someone is going to do it, and that could be you, but I don’t think there is, because you can run Linux on Windows pretty efficiently or probably in your docker containers. I don’t see why you should be very bothered about running PHP directly on windows&lt;/p&gt;

</description>
    </item>
    <item>
      <title>ReactJS — useEffect() &amp; useCallback()</title>
      <dc:creator>Paul Edward</dc:creator>
      <pubDate>Mon, 02 Dec 2024 09:36:37 +0000</pubDate>
      <link>https://dev.to/infinitypaul/reactjs-useeffect-usecallback-2l0f</link>
      <guid>https://dev.to/infinitypaul/reactjs-useeffect-usecallback-2l0f</guid>
      <description>&lt;h2&gt;
  
  
  ReactJS — useEffect() &amp;amp; useCallback()
&lt;/h2&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%2Fcdn-images-1.medium.com%2Fmax%2F7764%2F0%2A_whxA4CEuPShuvpS" 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%2Fcdn-images-1.medium.com%2Fmax%2F7764%2F0%2A_whxA4CEuPShuvpS" alt="Photo by [Christopher Gower](https://unsplash.com/@cgower?utm_source=medium&amp;amp;utm_medium=referral) on [Unsplash](https://unsplash.com?utm_source=medium&amp;amp;utm_medium=referral)" width="800" height="532"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In case you never heard about useCallback() and useEffect(), I strongly recommend that you check the official docs listed below&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;React in general: &lt;a href="https://reactjs.org/docs/getting-started.html" rel="noopener noreferrer"&gt;https://reactjs.org/docs/getting-started.html&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;useEffect(): &lt;a href="https://reactjs.org/docs/hooks-reference.html#useeffect" rel="noopener noreferrer"&gt;https://reactjs.org/docs/hooks-reference.html#useeffect&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;useCallback(): &lt;a href="https://reactjs.org/docs/hooks-reference.html#usecallback" rel="noopener noreferrer"&gt;https://reactjs.org/docs/hooks-reference.html#usecallback&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In Summary:&lt;/p&gt;

&lt;h2&gt;
  
  
  useEffect()
&lt;/h2&gt;

&lt;p&gt;useEffect() is a &lt;a href="https://reactjs.org/docs/hooks-intro.html" rel="noopener noreferrer"&gt;React Hook&lt;/a&gt; which allows you to handle &lt;strong&gt;side effects&lt;/strong&gt; in your functional React components.&lt;/p&gt;

&lt;p&gt;You can use it to do anything that doesn’t directly impact your UI/ JSX code (it might eventually affect it, for example, if you’re fetching data from some server, but for the current render cycle, it will not).&lt;/p&gt;

&lt;p&gt;useEffect() allows you to register a function which executes &lt;strong&gt;AFTER the current render cycle&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;useEffect() runs after every render cycle (i.e. whenever your functional component re-runs/ re-renders), unless you pass a second argument to an &lt;strong&gt;array of dependencies&lt;/strong&gt; of the effect.&lt;/p&gt;

&lt;p&gt;With such a dependency array provided, useEffect() will &lt;strong&gt;only re-run&lt;/strong&gt; the function you passed as a first argument, &lt;strong&gt;whenever one of the dependencies changed&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  useCallback()
&lt;/h2&gt;

&lt;p&gt;useCallback() often is used in conjunction with useEffect() because it allows you to prevent the re-creation of a function. For this, it's important to understand that &lt;strong&gt;functions are just objects in&lt;/strong&gt; JavaScript.&lt;/p&gt;

&lt;p&gt;Therefore, if you have a function (A) inside of a function (B), the inner function (=A) will be recreated (i.e. a brand-new object is created) whenever the outer function (B) runs.&lt;/p&gt;

&lt;p&gt;That means that in a functional component, &lt;strong&gt;any function you define inside of it is re-created whenever the component rebuilds&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Example:&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
Typically, it’s not a problem, that innerFunction is re-created for every render cycle.

&lt;p&gt;But it becomes a problem if innerFunction is a dependency of useEffect():&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
Why is the above code problematic?

&lt;p&gt;The effect re-runs whenever innerFunction changes. As stated, it is re-created whenever MyComponent re-builds&lt;/p&gt;

&lt;p&gt;Because functions are objects and objects are reference types, that means that the effect will re-run for every render cycle&lt;/p&gt;

&lt;p&gt;That might still not be a huge problem, but it is, if innerFunction does something that causes MyComponent to re-build (i.e. if it either does something that changes the props or the state).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Now, you would have an infinite loop!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;useCallback() helps you prevent this.&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;
&lt;br&gt;
By wrapping it around a function declaration and defining the dependencies of the function, it ensures that the function is &lt;strong&gt;only re-created if its dependencies changed&lt;/strong&gt;.

&lt;p&gt;&lt;strong&gt;Hence the function is NOT re-built on every render cycle anymore&lt;/strong&gt; =&amp;gt; You break out of the infinite loop!&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
