<?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 aungwin thant on Medium]]></title>
        <description><![CDATA[Stories by aungwin thant on Medium]]></description>
        <link>https://medium.com/@aungwinthant?source=rss-182c06112ce6------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*D7eJKEURL0sfRfnnkaAVFg.jpeg</url>
            <title>Stories by aungwin thant on Medium</title>
            <link>https://medium.com/@aungwinthant?source=rss-182c06112ce6------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 12:05:03 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@aungwinthant/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[How Laravel api resource class have access to underlying model?]]></title>
            <link>https://medium.com/@aungwinthant/how-laravel-api-resource-class-have-access-to-underlying-model-3574b6f29808?source=rss-182c06112ce6------2</link>
            <guid isPermaLink="false">https://medium.com/p/3574b6f29808</guid>
            <category><![CDATA[resources]]></category>
            <category><![CDATA[api]]></category>
            <category><![CDATA[how-to]]></category>
            <category><![CDATA[laravel]]></category>
            <category><![CDATA[php]]></category>
            <dc:creator><![CDATA[aungwin thant]]></dc:creator>
            <pubDate>Fri, 10 Jan 2020 18:20:24 GMT</pubDate>
            <atom:updated>2020-01-10T18:20:24.452Z</atom:updated>
            <content:encoded><![CDATA[<h3>How laravel api resource class have access to underlying model?</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*tgl5lC8-moBuSYrI" /><figcaption>Photo by <a href="https://unsplash.com/@rxspawn?utm_source=medium&amp;utm_medium=referral">Florian Olivo</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>As a laravel backend developer, API resources are my everyday friends. API resources are very convenient in a way that they can treat underlying model’s attributes and methods as if they were their own. In the documentation about api resources it says</p><blockquote>This is because a resource class will automatically proxy property and method access down to the underlying model for convenient access.<br>ref : <a href="https://laravel.com/docs/6.x/eloquent-resources">laravel documentation</a></blockquote><p>So firstly how does api resource class get access to underlying model class?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/583/1*woqAAtFL_9ZcWg94ShkgOQ.png" /><figcaption>Too lazy to do it with github gist. So I screenshot.</figcaption></figure><p>So here you can see resource class extends JsonResource class.</p><p>JsonResource class has a constructor in it and the constructor accepts a parameter and set it to the resource variable.</p><p>JsonResource class also extends two traits- ConditionallyLoadsAttributes and DelegatesToResource traits.</p><p>DelegatesToResource Trait is the main reason why ResourceClass can have access to underlying attributes and methods because it has _get() and _call() php magic methods.</p><p>Let me explain it in code to those who are still wondering. <br>In php, _get() magic method is used for reading inaccessible or non-exists properties.</p><p>Let’s build three <strong>plain php </strong>classes for example called- Controller.php, Resource.php, Model.php.</p><p><strong>**Note: The class names can be anything. I am using these names just to be same as in the laravel’s case.**</strong></p><p>First add some fields in Model.php</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/372/1*M_n1GuUDRnYawmuV4M80HA.png" /></figure><p>next, we need resource class to proxy that model.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/475/1*U9CGz2MZC_4QuWQcBeUb-w.png" /></figure><p>Resource.php has a resource property, a constructor that accepts a resource, __get() magic method and toArray() method.</p><p>Next, we need to implement Controller.php</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/447/1*KRjkfHgZRmuKfmum0Dq72g.png" /></figure><p>As you can see, the class is pretty straight forward. I called the toArray() method of the Resource.php class manually on the controller. <br>In the toArray() method of the Resource class, it returns an array containing the name attribute which is a member of Resource class. But in my __get() magic method I have return <strong>$this-&gt;resource-&gt;name</strong>. Currently <strong>$this-&gt;resource</strong> is the instance of Model class. Therefore my Resource.php class’s $resource attribute can access Model’s attributes.</p><p>Finally we can test it by writing an index.php file.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/339/1*vP7X528WuX90woteWEN5MQ.png" /></figure><p>When you run the index.php, you will see an array with a string containing values from Model class will be output.</p><p>Therefore laravel’s api resource classes have access to underlying model class by implementing __get() magic method.</p><p>Thanks alot for reading.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3574b6f29808" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Repository Pattern with Laravel]]></title>
            <link>https://medium.com/@aungwinthant/repository-pattern-with-laravel-5b1d64442f85?source=rss-182c06112ce6------2</link>
            <guid isPermaLink="false">https://medium.com/p/5b1d64442f85</guid>
            <category><![CDATA[laravel]]></category>
            <category><![CDATA[repository-pattern]]></category>
            <category><![CDATA[php]]></category>
            <dc:creator><![CDATA[aungwin thant]]></dc:creator>
            <pubDate>Mon, 16 Sep 2019 06:01:01 GMT</pubDate>
            <atom:updated>2019-09-16T06:01:01.119Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="Earth’s Atmosphere" src="https://cdn-images-1.medium.com/max/1024/0*7bOKxDH3OaeDipoj" /><figcaption>Photo by <a href="https://unsplash.com/@nasa?utm_source=medium&amp;utm_medium=referral">NASA</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>Introduction</h3><p>Repository ဆို တာ Data Persistence Layer တစ်ခုသာဖြစ်ပါတယ်။ Business Logic တွေနဲ့ Data Mapping Layer ရဲ့ ကြားခံနယ်တစ်ခုဆိုလည်း မမှားပါဘူး။ ယေဘူယျ အားဖြင့် Repository class တွေရဲ့ လုပ်ဆောင်ချက်က Business Logic Class တွေ တောင်းဆိုတဲ့ Query တွေကို လိုချင်တဲ့အတိုင်း ပြန်ပို့ပေးတာပဲဖြစ်ပါတယ်။ Database ကရလာတဲ့ Query ကို Model Class က သူ့ရဲ့ fields တွေနဲ့ Map လုပ်ပါတယ်။ Map လုပ်ပြီးသား result တွေကို collection ဒါမှမဟုတ် တခြား သက်ဆိုင်ရာ data type အနေနဲ့ ပြန်ပို့ပါတယ်။ Repository တစ်ခုရှိထားတာကြောင့် Business Logic Class တွေဟာ Data တွေက ဘယ် Source ကလာသလည်းဆိုတာကို ဂရုစိုက်စရာမလိုတော့ပါဘူး။ အဲဒီအတွက် ဘာအကျိုးကျေးဇူးတွေရသွားလည်းဆိုတာတော့ တစ်ကယ်လို့ Project Manager က MySQL မသုံးချင်ဘူး MongoDB သုံးရအောင်ဆိုရင် ကျွန်တော်တို့ က Repository ရဲ့ Dependency မှာ Eloquent ကို extends လုပ်ထားတဲ့ Model Class အစား MongoDB ကို implement လုပ်ထားတဲ့ Model Class ကို ထည့်လိုက်ရုံပဲဖြစ်ပါတယ်။</p><p>Laravel မှာ Reporitory Pattern လို့ရှာလိုက်ရင် articles တွေအများကြီးပါပဲ။ Repository ကိုသုံးဖို့လိုလားဆိုတာတော့ အခုထိ debate လုပ်နေကြတုန်းပါ။ အရင်တုန်းက Laravel Community မှာ Repository Pattern ကို အသုံးပြုကြဖို့ တိုက်တွန်းကြပါတယ်။ နောက်ပိုင်းမှာ Eloquent ရဲ့ Features တွေအရမ်းကောင်းလာတော့ Eloquent Model Class ကိုပဲ Database Abstraction Layer တစ်ခုအနေနဲ့ပဲသုံးလာပါတော့တယ်။</p><p>အဲဒါဆို ကျွန်တော်တို့ Laravel မှာ Repository ကို ဘယ်လိုသုံးရင်ကောင်းမလည်း…….</p><p>ကျွန်တော့်ရဲ့ အယူအဆကတော့ Laravel ရဲ့ Eloquent Model Class က အရမ်းကောင်းနေပြီးသား Database Abstraction ပေးပြီးသားဖြစ်ပါတယ်။ Fat Model, Thin Controller Concept နဲ့တင်လုံလောက်ပါတယ်။ တစ်ကယ်လို့ Project က ကြီးလာတဲ့အခါ Model ကအရမ်း Fat လာမှသာ သုံးဖို့လိုမယ်လို့ထင်ပါတယ်။ small projects တွေမှာတော့ မလိုပါဘူး။ တစ်ကယ်လို့ Caching တွေသုံးမယ် Code ပြင်ရတာများလာတယ်ဆိုရင် Repository Pattern သုံးရင် ပြင်ရတာ အဆင်ပြေပါတယ်။ တစ်ကယ်လို့ Customer က feature အသစ်တိုးချင်လို့ Database မှာ fields အသစ်တွေထပ်တိုးရတဲ့အခါ တစ်နေရာထဲမှာပြင်ရုံနဲ့ ကျန်တဲ့နေရာတွေပါ အဆင်ပြေသွားမယ်ဆိုရင် အချိန်ကုန်သက်သာပါတယ်။</p><p>အဲဒါဆို Repository သုံးပြီးတစ်ခုလောက်ရေးကြည့်ရအောင်။</p><h3>Demo</h3><p>ကျွန်တော်ကတော့ Category, Post, Comment Model သုံးခုကို ဥပမာအနေနဲ့အသုံးပြုသွားပါမယ်။ Model သုံးခုအထဲကမှာ Category တစ်ခုကို ပဲ Repository သုံးပြီးရေးမှာဖြစ်ပါတယ်။ ကျန်တာတော့ အတူတူပဲမို့ဆက်မရေးတော့ပါဘူး။ (မရေးရင် ဘာလို့ model တွေထည့်နေရတာတုန်းဆိုတော့ လက်လွန်သွားလို့ပါ)</p><p>ပထမအနေနဲ့ Repository folder တစ်ခု ပြုလုပ်လိုက်ပါတယ်။ ပြီးတော့<br>Abstract Repository Class တစ်ခုတည်ဆောက်လိုက်ပါတယ်။ Model တော်တော်များများရဲ့ list, create, update, delete method တွေက တူတာများတဲ့အတွက်ပါ။ တစ်ကယ်လို့ မတူရင်လည်း override ပြီးပြန်ရေးလို့ရပါတယ်။</p><p>အဲ့တော့ project structure က ဒီလိုဖြစ်သွားပါတယ်။</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/241/1*Gyhkll3LlKCDyg9Nxx0vrQ.png" /><figcaption>Project Structure</figcaption></figure><p>**Notice : Models က folder မလုပ်လည်းရပါတယ်။**</p><p>Abstract Repository မှာ အောက်ပါအတိုင်း ရေးထားပါတယ်။</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b1c57e902607578d106c87433d75f41b/href">https://medium.com/media/b1c57e902607578d106c87433d75f41b/href</a></iframe><p>**ကျန်တဲ့ function တွေကို လိုအပ်သလို ထပ်ရေးလို့ရပါတယ်။**</p><p>နောက်တစ်ခု က interface တစ်ခုရေးပါမယ်။ Interface ရေးတဲ့ အကြောင်းက software development နယ်ပယ်မှာ “Program to Interface, Not To Implementation” ဆိုတာရှိပါတယ်။ ကျွန်တော်တို့ရဲ့ Repository ကို အခြား Class တွေက သုံးတဲ့ အခါ interface မှာ သတ်မှတ်ထားတဲ့ functions တွေကိုပဲ ခေါ်ပြီးအသုံးပြုနိုင်မှာဖြစ်ပါတယ်။ Business Logic Class ဒါမှမဟုတ် Controllers တွေက လိုအပ်မယ်ထင်တဲ့ functions တွေကို interface ထဲမှာ သတ်မှတ်ပေးထားတဲ့ သဘောပါပဲ။ function တွေခေါ်သုံးတဲ့အခါ interface(contract) ကို ခေါ် တစ်ကယ် implementation က interface ကို implement လုပ်တဲ့ concrete class ထဲမှာ လုပ်ဆိုတာမျိုးဖြစ်ပါတယ်။ ဒါဆို Category Model အတွက် CategoryRepositoryInterface တစ်ခုကိုတည်ဆောက်လိုက်ပါမယ်။</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/04fa84d2c8686dc8025e7d676af83ab2/href">https://medium.com/media/04fa84d2c8686dc8025e7d676af83ab2/href</a></iframe><p>Interface (Contract) ရေးပြီးတဲ့အခါမှာ Repository Concrete ကို ရေးပါမယ်။</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d6b88593c478678b8437081d63bd8a64/href">https://medium.com/media/d6b88593c478678b8437081d63bd8a64/href</a></iframe><p>Concrete Class ဟာ အပေါ်က abstract Repository ကို extends လုပ်ထားပါတယ်။ တစ်ချို့ တူညီတဲ့ functions တွေကို abstract ထဲမှာ ရေးထားတဲ့အတွက် concrete ထဲမှာ ထပ်ရေးစရာမလိုတော့ပါဘူး။ တစ်ကယ်လို့ လိုအပ်ရင် concrete ထဲမှာ override လုပ်ပြီးရေးလို့ရပါတယ်။ CategoryRepository ထဲမှာ လိုအပ်တဲ့ တစ်ခြား complex query တွေကို ရေးနိုင်ပါတယ်။ ကျွန်တော်ကတော့ model တွေထဲမှာ လိုအပ်တဲ့ query scope တွေသတ်မှတ်လိုက်ပါတယ်။ ပြီးတော့ repository ထဲမှာ ပြန်ပေါင်းပြီးရေးပါတယ်။ ဥပမာ User Model ထဲမှာ isActive ဆိုတဲ့ scope ရယ်, byDate ဆိုတဲ့ scope နှစ်ခုသတ်မှတ်ထားပါတယ်။ Active By Date ကို UserRepository ထဲမှာသွားရေးလိုက်ပါတယ်။ ဒါကျွန်တော်သုံးတဲ့ပုံစံပါ။</p><p>နောက်ဆုံးအဆင့်အနေနဲ့ Laravel ရဲ့ Service Container ကို သုံးပြီး Contract နဲ့ Concreate ကို bind လုပ်ပါမယ်။ အဲဒီအတွက်</p><p>AppServiceProvider ထဲ register method ထဲမှာ အောက်ပါအတိုင်း ဝင်ရောက် bind နိုင်ပါတယ်။</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0d256db0507717df24684b12e89e9006/href">https://medium.com/media/0d256db0507717df24684b12e89e9006/href</a></iframe><p>အဲဒါဆို ကျွန်တော်တို့ရဲ့ Concrete နဲ့ Contract class တွေကို စတင်ပြီး အသုံးပြုလို့ရပါပြီ။</p><h3><strong>Testing</strong></h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/88a46dc33ddb8a6fb34002dc3a9ee375/href">https://medium.com/media/88a46dc33ddb8a6fb34002dc3a9ee375/href</a></iframe><p>တစ်ကယ်လို့ Controller ထဲမှာ ထည့်ပြီးစမ်းချင်ရင် Contract (Interface) ကို Dependency အနေနဲ့ ယူပြီး ထည့်သုံးနိုင်ပါတယ်။</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3ebc254d07e60313dd98a76ff3de6c4b/href">https://medium.com/media/3ebc254d07e60313dd98a76ff3de6c4b/href</a></iframe><p>Testing save category feature</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/aad20ec8471b18246a5c3dd24c0f22e5/href">https://medium.com/media/aad20ec8471b18246a5c3dd24c0f22e5/href</a></iframe><p>Test Results</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/811/1*XJux1OWyXewO5TS66hrKcw.png" /><figcaption>Test results</figcaption></figure><h3>Conclusion</h3><p>တစ်ချိို့သူတွေကတော့ Repository ကိုသုံးတာ overkill ဖြစ်တယ်ပြောကြပါတယ်။ တစ်ချို့ကလည်း ဒီလိုသုံးတာပိုပြီး စနစ်ကျတယ်ပြောကြပါတယ်။ ကျွန်တော့်အနေနဲ့ တော့ Repository ကို လိုအပ်တဲ့ Project တွေမှာ သုံးပါတယ်။ ဥပမာ Data Source ကို Database တစ်မျိုးတည်းမသုံးတဲ့ Project, Code Maintain လုပ်ရတာ ခက်မယ့် Project မျိုး။ Complex ဖြစ်တဲ့ Project မျိုးမှာ သုံးပါတယ်။ မလိုအပ်ရင်တော့ <strong><em>Fat Model Thin Controller</em></strong> နဲ့ပဲ သုံးပြီးရေးပါတယ်။ အခုလောလောဆယ်အထိ Repository Pattern နဲ့ရေးတာ ဘာအကျိုးကျေးဇူးရလည်းဆိုတော့ တစ်ခုခုပြင်ချင်ရင် trace လိုက်ရတာမြန်ပါတယ်။ Model မှာ field တစ်ခုတိုးလို့ တစ်ခုခုပြင်ချင်ရင် repository ထဲပဲသွားပြင်လိုက်ပါတယ်။ ပြီးရင် test run လိုက်ပါတယ်။ ကျန်တဲ့ code base ကို မထိတော့ဘူး။ ကျွန်တော် နားလည်သလောက် Repository Pattern ကတော့ ဒီလောက်ပါပဲ။ လိုအပ်တာတွေရှိရင်လည်း ဝေဖန်ထောက်ပြနိုင်ပါတယ်။ အခုလို အချိန်ပေးပြီးဖတ်ပေးတဲ့အတွက် ကျေးဇူးတင်ပါတယ်။</p><p>Code : <a href="https://github.com/aungwinthant/repository-pattern">https://github.com/aungwinthant/repository-pattern</a></p><p>References</p><ol><li><a href="https://vegibit.com/laravel-repository-pattern/">https://vegibit.com/laravel-repository-pattern/</a></li><li><a href="https://www.larashout.com/how-to-use-repository-pattern-in-laravel">https://www.larashout.com/how-to-use-repository-pattern-in-laravel</a></li><li><a href="https://www.laravelbestpractices.com/#Design%20Patterns">https://www.laravelbestpractices.com/#Design%20Patterns</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5b1d64442f85" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>