<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Solaiman Khan on Medium]]></title>
        <description><![CDATA[Stories by Solaiman Khan on Medium]]></description>
        <link>https://medium.com/@solaiman?source=rss-9eacd9ef60bd------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*EEIctiEaMCq33tgdCD4O-w.png</url>
            <title>Stories by Solaiman Khan on Medium</title>
            <link>https://medium.com/@solaiman?source=rss-9eacd9ef60bd------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 20:20:15 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@solaiman/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[A Deep Understanding of Laravel One To One Relationship]]></title>
            <link>https://solaiman.medium.com/a-deep-understanding-of-laravel-one-to-one-relationship-4fda8c86a7dc?source=rss-9eacd9ef60bd------2</link>
            <guid isPermaLink="false">https://medium.com/p/4fda8c86a7dc</guid>
            <category><![CDATA[laravel]]></category>
            <category><![CDATA[laravel-relationships]]></category>
            <category><![CDATA[laravel-development]]></category>
            <category><![CDATA[relationships]]></category>
            <category><![CDATA[one-to-one]]></category>
            <dc:creator><![CDATA[Solaiman Khan]]></dc:creator>
            <pubDate>Fri, 27 Aug 2021 08:33:15 GMT</pubDate>
            <atom:updated>2021-08-27T10:20:30.925Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*00tRo2l6K6kg5B0kgui0fg.jpeg" /></figure><p>One to One relationship are the easiest relationship in Laravel Eloquent Relationships. However many people struggle to understand especially the inverse part, belongsTo to be exact. In this article i will briefly describe both way of this relationship.</p><p><strong>#Table &amp; Its Stecture</strong><br>Suppose I have two Model one is User and another is Phone. Table Stecture are given below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xhv-VanH1ZdpHNg_pARbTA.png" /></figure><p>!Important — Here both table has id as primary key.</p><h3><strong>#HasOne</strong></h3><blockquote>A User has a phone number, so it relation type will be hasOne.<br><em>Lets define that in ‘</em><strong><em>UserModel’</em></strong></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/856/1*sdRSuMV3pztpcy2lGrNUjg.png" /></figure><p>Here, Laravel Eloquent determines the foreign key of the relationship based on the parent model name. In this case, the Phone model is automatically assumed to have a user_id foreign key as our table stecture describe.</p><p>Great going, lets dive deep..</p><p>If our <strong>PhoneModel </strong>does not have a foreign key following the naming convention. If our <strong>PhoneModel </strong>has a different name rather than user_id?</p><p>Lets assume our <strong>PhoneModel </strong>has a foreign key column named abcd_id Then you can pass that column name in second parameter.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/992/1*y91O4g1LZFN03laog0BdBg.png" /></figure><p>In this case our <strong>UserModel </strong>has a foreign key column name is phone_id</p><p>Now another senario is if our UserModel has a different name of xyz_idrather than phone_idthan what? No worries, you can pass it as a third parameter.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*os7K1R9FQKyBxQXXssRcSg.png" /></figure><p>This pattern happens under the hood. Laravel do it for you but if there is a different naming convention other than laravel then you have to explicitly say it.</p><h3>#BelongsTo</h3><blockquote>A Phone is belongs to a user, so it relation type will be inverse of hasOne named<em>belongsTo.<br>Lets define that in ‘</em><strong><em>PhoneModel’</em></strong></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/866/1*_Kg0_HyuFELbPVTyn0AfHA.png" /></figure><p>Here Laravel Eloquent will attempt to find a <strong>UserModel </strong>that has an id which matches the user_id column on the <strong>PhoneModel</strong>.</p><p>We are done but lets go beyond this..</p><p>Lets Assume our <strong>UserModel</strong> has a different primary key nameduid rather than idalso the <strong>PhoneModel </strong>has a other foreign key column namedxxxx_id rather thanuser_id.Look at the given picture.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oCUJav57jvo0-RND1_JfUw.png" /></figure><p>Don’t worry, you can pass both this name as a parameter likexxxx_idas second parameter and uidas third parameter.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Fj8eB4LG3YIjzoAwzxFTWw.png" /></figure><p>If you do not have a <strong>UserModel</strong> with a different primary key rather thanid then you don’t need to pass as third parameter. Done!</p><p>I hope that this article gave you a brief understanding of easy yet tricky relationship in laravel eloquent relationships. Feel free to leave a comment if you have any feedback, questions and thanks for reading.</p><p>Jazāk Allāhu Khayran!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4fda8c86a7dc" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A Guide To Not Repeat Common Laravel Relationships In Model Using Traits]]></title>
            <link>https://solaiman.medium.com/a-guide-to-not-repeat-common-laravel-relationships-in-model-using-traits-254275a3cff1?source=rss-9eacd9ef60bd------2</link>
            <guid isPermaLink="false">https://medium.com/p/254275a3cff1</guid>
            <category><![CDATA[eloquent]]></category>
            <category><![CDATA[laravel]]></category>
            <category><![CDATA[traits]]></category>
            <category><![CDATA[relationships]]></category>
            <category><![CDATA[clean-code]]></category>
            <dc:creator><![CDATA[Solaiman Khan]]></dc:creator>
            <pubDate>Sat, 21 Aug 2021 05:21:19 GMT</pubDate>
            <atom:updated>2021-08-21T05:21:19.627Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/810/1*sJ9W_qDZ8Y99oygSLvBVog.png" /></figure><p>Defining relationships in Laravel is something that every Laravel developer probably has done more than once. But if you define same relationships in almost every model, that would be repetitation of work, what if we have a common file and it use by every model whether it has some common properties to use or on demand use to be precise.</p><h3>#Example:</h3><p>Most of the cases your tables might need a ‘<em>Created By</em>’ column to identify who created it and when subsquently like ‘<em>Created At</em>’ for that you need to add this two column to your migration and to access the full eloquent feature you often need to define relation in every model for that and that a whole repeated work to do. What if we have a common file with the desire relationship defined and use in every model to access the relation on to go. We just have to write once and that it. Use and enjoy in every model.</p><p>Lets dive into it</p><h3><strong>#Step 1:</strong></h3><ol><li>Create a directory on your app directory name ‘<strong>Traits</strong>’ here you will put all your traits files.<br><strong><em>note: </em></strong><em>Its a personal preference, if you wish to organize then do it, otherwise the app directory is fine.</em></li></ol><figure><img alt="create a dir to app directory." src="https://cdn-images-1.medium.com/max/626/1*tZHFXtapA-O52M80R2rjRw.png" /></figure><h3><strong>#Step 2:</strong></h3><p>Create a php file name ‘<strong><em>YourTraitFileName.php</em></strong>’ in my case its <strong><em>CommonTrait.php</em></strong></p><ol><li>Add a namespace (automated in few IDE).</li><li>Decleare a trait name ‘<strong><em>YourTraitName</em></strong>’ in my case its ‘<strong><em>CommonTrait’</em></strong>.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*W4nQUmKVg95Hylpy19rSPA.png" /></figure><h3><strong>#Step 3:</strong></h3><ol><li>Define a public function ‘<strong><em>relation name</em></strong>’, in my case ‘<strong><em>created_user</em></strong>’.</li><li>Now declear a <strong><em>hasOne </em></strong>relationship, in this case we have a <strong><em>User </em></strong>model which is ships with laravel by defaults.<br><strong><em>note:</em></strong><em> As Eloquent determines the foreign key of the relationship based on the parent model name, so we assume our </em><strong><em>ParentModel*</em></strong><em>has a column name </em><strong><em>user_id.</em></strong><em>*Here</em><strong><em>ParentModel</em></strong><em>is the model where we will use this trait. Also make sure the Models are relationships are imported at the top.</em></li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9GePDK9wk2VZ8JhwsAlusw.png" /></figure><p>3. But in my case i have a column name <strong>created_by</strong> in my <strong><em>ParentModel,</em></strong>to use that you have to say it explicitly. Like below image.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*csNvNFsIPyHKJdiWUHiXPg.png" /></figure><p>As laravel define</p><p>return $this-&gt;hasOne(Model, &#39;foreign_key&#39;, &#39;local_key&#39;);</p><h3>#Step 4:</h3><ol><li>Now use in your model. In my case its Invoice Model. <br><strong><em>note:</em></strong><em> </em><strong><em>ParentModel</em></strong><em> which is </em><strong><em>Invoice </em></strong><em>in my case.</em></li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SvLBWn1ihlXk55Q525C8aw.png" /></figure><h3>#Step 5:</h3><ol><li>Done!</li><li>Now use like as usual you use it. Wherever you need that use relationship just use the trait in your model and bamm.</li></ol><h3>#Additional:</h3><ol><li>You can also use this trait in many common issues of your model like, to get your table name you can use like this. This getTableName function will give you the table name.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1012/1*MnR8h_LfmzdMdYWlrTVwdA.png" /></figure><p>2. And many more common properties, i think you get the idea.</p><p>I hope that this article gave you a short yet comfortable solution to get rid of code repeating in different models and save a little bit of time. Feel free to leave a comment if you have any feedback, questions and thanks for reading.</p><p>Jazāk Allāhu Khayran!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=254275a3cff1" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>