<?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: codewitholgun</title>
    <description>The latest articles on DEV Community by codewitholgun (@codewitholgun).</description>
    <link>https://dev.to/codewitholgun</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%2F812582%2F928cf2d3-d2d9-4b84-96f7-393f819a2039.jpg</url>
      <title>DEV Community: codewitholgun</title>
      <link>https://dev.to/codewitholgun</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/codewitholgun"/>
    <language>en</language>
    <item>
      <title>Laravel Eloquent Tutorial - Get only the fields (aka columns) we need from relation.</title>
      <dc:creator>codewitholgun</dc:creator>
      <pubDate>Sun, 20 Feb 2022 01:10:16 +0000</pubDate>
      <link>https://dev.to/codewitholgun/laravel-eloquent-tutorial-get-only-the-fields-aka-columns-we-need-from-relation-5eee</link>
      <guid>https://dev.to/codewitholgun/laravel-eloquent-tutorial-get-only-the-fields-aka-columns-we-need-from-relation-5eee</guid>
      <description>&lt;p&gt;Laravel is a great framework to work with, and I use it every day for my 9-5 job. One of the greatest power of Laravel is, it has an ORM (Object Relational Mapping) as default called Eloquent.&lt;/p&gt;

&lt;p&gt;I am not going to talk about what is Eloquent ORM, but I would like to just indicate that, it makes your life easier to deal with databases.&lt;/p&gt;

&lt;p&gt;Below you can find some simple usages of eloquent;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$post = Post::find(1)
// That will run the following query in the database
// select * from posts where id = 1
// You see, it's so easy to use and powerful because we don't need to write a query anymore
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you are using relational databases such as MySQL, MariaDB, PostgreSQL and etc, it becomes more powerful because you don't need to write complex join queries anymore.&lt;/p&gt;

&lt;p&gt;For example, we want to get a specific post from the posts table but also we want to GET the author of the post as well. The following example, clearly explains that.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Post extends Model {
 ....
 // that will assume that we have author_id column in the posts table
 public function author(): BelongsTo
{ 
   return $this-&amp;gt;belongsTo(related: Author::class)
 }
}

 // Since the author relation is defined in Post model, we can load it using following code
$post = Post::with('author')-&amp;gt;find(1);
// Now, the post data will have the author data from database
// This is called Eager loading and it is very important learn about it.
// Link: https://laravel.com/docs/9.x/eloquent-relationships#eager-loading
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since we used eager loading to get author data with the post, all columns in the author data is loaded. This is not a problem with a small amount of data, but if we get millions of posts, each post will have an author and probably that will run a little bit slower. So, instead of getting all columns in author data, let's get some specific columns.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$post = Post::with('author:id, name')-&amp;gt;find(1);
// Now, the post data will have the author data from database but only specific columns id and name
// This will make your it more faster to load data
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the example above, we have used :id, name, so, only those columns are going to be loaded from the database.&lt;/p&gt;

&lt;p&gt;That's all for this tutorial :)&lt;/p&gt;

&lt;p&gt;If you like to get more content similar to that, please follow me on Twitter and on my youtube channel.&lt;/p&gt;

&lt;p&gt;Twitter: twitter.com/olgunsoftware&lt;/p&gt;

&lt;p&gt;Youtube: youtube.com/channel/UCH-xplaz-cmloIUkYspcEhQ&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>php</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
    <item>
      <title>How to create custom helpers in Laravel?</title>
      <dc:creator>codewitholgun</dc:creator>
      <pubDate>Thu, 10 Feb 2022 08:12:52 +0000</pubDate>
      <link>https://dev.to/codewitholgun/how-to-create-custom-helpers-in-laravel-357l</link>
      <guid>https://dev.to/codewitholgun/how-to-create-custom-helpers-in-laravel-357l</guid>
      <description>&lt;p&gt;Laravel is one of the best frameworks out there &lt;strong&gt;FOR ME&lt;/strong&gt;. I use it every day for my 9-5 job and I really love it.&lt;/p&gt;

&lt;p&gt;Whenever I start a new project either for my job or for my side projects, the first thing that I do is to create a new file called &lt;strong&gt;helpers.php&lt;/strong&gt;. I use that file to create the custom helper functions that I will be used for along the project.&lt;/p&gt;

&lt;p&gt;For example; I really don't like to find the name of the current route in blade files following a hard way like as below;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{{ Illuminate\Support\Facades\Route::currentRouteName() }}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Instead, I prefer to create a simple function that I can use anywhere in the project to get the current route name in a shorter syntax like as below;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{{ getCurrentRouteName() }} // In the blade files,also can be used in .php files as well.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  How to make sure that this simple function can work anywhere?
&lt;/h2&gt;

&lt;p&gt;Since Laravel as a default uses &lt;strong&gt;composer&lt;/strong&gt; to manage the dependencies, we have a powerful tool under our hands and we can tell the composer to autoload any file we want and it allows us to use it in the entire project.&lt;/p&gt;

&lt;p&gt;To do so; first of all, we have to create the file anywhere we want, as a default, I always put it under the &lt;strong&gt;app&lt;/strong&gt; folder.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;app/helpers.php&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;After I am sure that, the file exists, I create my own custom helper functions as shown below;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// function_exists method is initiated to check;
// If there is any function existed with the name of getCurrentRouteName,
// If not, this function will be added otherwise existed one will be used.
// The aim is to protect existing functions to be overwritten
if (!function_exists('getCurrentRouteName')) {
    function getCurrentRouteName()
    {
        return Illuminate\Support\Facades\Route::currentRouteName();
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At this point, I have to tell the composer to discover and autoload this file by adding it to the &lt;strong&gt;composer.json&lt;/strong&gt; file as shown below.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If you don't know, composer.json is a file in that you can define the project dependencies and install them using a simple command composer install or update them with composer update.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    "require": { ... },
    "require-dev": { ... },
    "autoload": {
        "psr-4": { ... },
        "files": [
            "app/helpers.php" //this is the place I tell composer to autoload file
        ]
    },
    ...
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I created a file, add it to the composer.json file, so is it going to work? &lt;strong&gt;Unfortunately No&lt;/strong&gt; 😥&lt;/p&gt;

&lt;p&gt;Because we have to run a simple composer command to load all files again in the entire project just for one time.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;composer dump-autoload
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I am afraid to ask, but is it going to work? &lt;strong&gt;Yes! Congratulations, you've made it&lt;/strong&gt; 😇&lt;/p&gt;

&lt;p&gt;So, now I can use getCurrentRouteName anywhere I want as I showed in the example above.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{{ getCurrentRouteName() }} // In the blade files,also can be used in .php files as well.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is how I create custom helpers in my Laravel projects.&lt;/p&gt;

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