<?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: benittobeny34</title>
    <description>The latest articles on DEV Community by benittobeny34 (@benittobeny34).</description>
    <link>https://dev.to/benittobeny34</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%2F903395%2Fd309b36c-0605-4b13-b6a4-e854bd7f71d2.JPEG</url>
      <title>DEV Community: benittobeny34</title>
      <link>https://dev.to/benittobeny34</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/benittobeny34"/>
    <language>en</language>
    <item>
      <title>Laravel: object _get Alternative for optional Helper Function</title>
      <dc:creator>benittobeny34</dc:creator>
      <pubDate>Sat, 13 Aug 2022 13:31:45 +0000</pubDate>
      <link>https://dev.to/benittobeny34/laravel-object-get-alternative-for-optional-helper-function-2e0o</link>
      <guid>https://dev.to/benittobeny34/laravel-object-get-alternative-for-optional-helper-function-2e0o</guid>
      <description>&lt;p&gt;&lt;strong&gt;Hi Devs&lt;/strong&gt;, In Laravel when accessing a relationship property we not sure whether the relationship data exists. if exists it's good to go. but what if not exists those times we get the following Warning;&lt;/p&gt;

&lt;p&gt;Consider the Below code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$user = User::findOrFail(1);
$subscription = $user-&amp;gt;subscription-&amp;gt;created_at;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;PHP Notice:  Trying to get property 'created_at' of non-object in /Users/benittoraj/code/laravel/mas-laraveleval()'d code on line&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;To Avoid the above error we often use optional helper function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; $subscriptionCreatedAt= optional($user-&amp;gt;subscription)-&amp;gt;created_at;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Do You know there is another way of handling this using object_get helper function&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$subscriptionCreatedAt = object_get($user, 'subscription.created_at);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Nice thing about object_get helper function is we can access the relationship relation property using dot syntax. So our code looks clean. If some relation data does't exists it stops there and return null;&lt;/p&gt;

&lt;p&gt;[object_get helper implementation](&lt;a href="https://github.com/illuminate/support/blob/master/helpers.php#:%7E:text=*/-,function%20object_get(%24object%2C%20%24key%2C%20%24default,%7D,-%7D)"&gt;https://github.com/illuminate/support/blob/master/helpers.php#:~:text=*/-,function%20object_get(%24object%2C%20%24key%2C%20%24default,%7D,-%7D)&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Keep Learning!!&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>helpers</category>
      <category>tips</category>
      <category>php</category>
    </item>
    <item>
      <title>Laravel: Log Sql Query With It's Bindings</title>
      <dc:creator>benittobeny34</dc:creator>
      <pubDate>Fri, 05 Aug 2022 02:27:15 +0000</pubDate>
      <link>https://dev.to/benittobeny34/laravel-log-sql-query-with-its-bindings-5eag</link>
      <guid>https://dev.to/benittobeny34/laravel-log-sql-query-with-its-bindings-5eag</guid>
      <description>&lt;p&gt;Most often laravel dev prefer to log the sql query by using below snippet.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DB::listen(function ($query) {
    info('Query', [
    "Query" =&amp;gt; $query-&amp;gt;sql,
    "Bindings" =&amp;gt; $query-&amp;gt;bindings,
    "Time" =&amp;gt; $query-&amp;gt;time,
  ]);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But the drawback of this is it split out the query, sql bindings and it's time. &lt;/p&gt;

&lt;p&gt;we can do it in a better way to combine the query and it's bindings by using &lt;code&gt;laravel&lt;/code&gt; Macroable Trait.&lt;/p&gt;

&lt;p&gt;Add the Below snippet in &lt;code&gt;AppServiceProvider&lt;/code&gt; and you are good to go.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Builder::macro('toSqlWithBindings', function () {
  $bindings = array_map(fn($value) =&amp;gt; 
                is_numeric($value) ? $value : "'{$value}'",
                $this-&amp;gt;getBindings()
            );
            return Str::replaceArray(
             '?', $bindings, $this-&amp;gt;toSql()
           );
        });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Most of the laravel illuminate classes are by default uses Macroable Trait. By Using that we can add our custom function to that illuminate classes. These functions are available as class function. so we can here chain the custom function to query builder.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$query = User::where('name', 'like' , '%admin%');

$query-&amp;gt;toSqlWithBindings();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;em&gt;Output&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;select * from `users` where `name` like '%admin%' and `users`.`deleted_at` is null`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Keep Learning&lt;/strong&gt;!!&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>mysql</category>
      <category>builder</category>
      <category>macro</category>
    </item>
    <item>
      <title>Laravel tap helper function</title>
      <dc:creator>benittobeny34</dc:creator>
      <pubDate>Thu, 04 Aug 2022 15:49:00 +0000</pubDate>
      <link>https://dev.to/benittobeny34/laravel-tap-helper-function-jof</link>
      <guid>https://dev.to/benittobeny34/laravel-tap-helper-function-jof</guid>
      <description>&lt;p&gt;Laravel's &lt;strong&gt;&lt;em&gt;tap()&lt;/em&gt;&lt;/strong&gt; is one of the simplest methods in the framework. pass in a value and a a closure , and you'll get back the modified original value. Cleans up code by eliminating temporary variables!&lt;/p&gt;

&lt;h2&gt;
  
  
  Here are some usage code snippets
&lt;/h2&gt;

&lt;p&gt;-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$user = tap(User::create(['id' =&amp;gt; 1, 'name'=&amp;gt; 'raj'])
-&amp;gt;sendWelcomeNotification();

//The nice thing about above code is it create the user and sends notification and returns the result of first argument.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$user = User::find(1);

$user-&amp;gt;update(['name' =&amp;gt; 'benitto']);

return $user;

// we can do this in one line

return tap($user)-&amp;gt;update(['name'=&amp;gt; 'benitto']);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
if we pass closure as second arguement the result of first arguement is passed to clousure
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$user = tap($user,function($user) {
       $user-&amp;gt;sendWelcomeNotification();
       $user-&amp;gt;onBoardSetup();
  });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;em&gt;Keep Learning!!&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>laravelhelpers</category>
      <category>functions</category>
    </item>
  </channel>
</rss>
