<?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 Unseen Heroes on Medium]]></title>
        <description><![CDATA[Stories by Unseen Heroes on Medium]]></description>
        <link>https://medium.com/@unseen.heroes?source=rss-6b5a6ea0e452------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*iVg8rZgUUiYQwBQvMKSLgA.png</url>
            <title>Stories by Unseen Heroes on Medium</title>
            <link>https://medium.com/@unseen.heroes?source=rss-6b5a6ea0e452------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 13:40:53 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@unseen.heroes/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 to solve the n+1 problem created by fractal]]></title>
            <link>https://medium.com/@unseen.heroes/how-to-solve-the-n-1-problem-introduced-by-fractal-7ba7936b8b81?source=rss-6b5a6ea0e452------2</link>
            <guid isPermaLink="false">https://medium.com/p/7ba7936b8b81</guid>
            <category><![CDATA[laravel]]></category>
            <category><![CDATA[resources]]></category>
            <category><![CDATA[lazy-loading]]></category>
            <category><![CDATA[eloquent]]></category>
            <category><![CDATA[fractals]]></category>
            <dc:creator><![CDATA[Unseen Heroes]]></dc:creator>
            <pubDate>Tue, 22 May 2018 23:34:43 GMT</pubDate>
            <atom:updated>2020-02-26T05:58:07.848Z</atom:updated>
            <content:encoded><![CDATA[<p>The popular package, <a href="http://fractal.thephpleague.com/">thephpleague/fractal</a> aims to protect your API consumers from output inconsistencies and also provide an elegant way to include relationships for complex data structures.</p><pre>example.com?include=foo,foo.bar,foo.bar.baz,bla <br>for 100 records per page == 400 queries!</pre><p>The problem with the includes is that all relations are being lazily loaded.</p><blockquote><strong>TL;DR</strong> use Laravel 5.5<em>+</em> Eloquent Resources and Spatie’s query builder</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nx9zVxRnxVDaN2pHabQ5Rg.png" /><figcaption>The Fractal transformer</figcaption></figure><p>The getId() , getContent() getters were created manually inside the Review model, in order to decouple database schema from code.</p><p>For this example, the includes may look like:</p><pre>example.com/user/101/reviews?include=product,product.merchant</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*svNlmkEgRQgMsUzhYEb-Mg.png" /><figcaption>example.com/user/101/reviews?include=product,product.merchant</figcaption></figure><p>Besides the nested data key, the additional database queries are killing the performance.</p><p>There are some workarounds, for example, we could <strong>use eagerly loaded</strong> relations, but the question is “how many?”. What happens when the front-end requires more includes? Do we eager load everything, even nested relations?</p><p><strong>Lazy eager load</strong> means more than one request and, moreover, you have to implement it manually, to override the TransformerAbstract class, callIncludeMethod , after fractal has checked that a given include is valid.</p><p><strong>Joins are too expensive</strong>, especially for large tables.</p><blockquote><strong>The solution</strong> I found consists of this couple:</blockquote><blockquote>Laravel 5.5 introduced Eloquent Resources, which basically are a sort of <strong>native transformers on steroids</strong>: <a href="https://laravel.com/docs/5.6/eloquent-resources#conditional-relationships">https://laravel.com/docs/5.6/eloquent-resources#conditional-relationships</a></blockquote><blockquote>Spatie’s query builder <a href="https://github.com/spatie/laravel-query-builder">https://github.com/spatie/laravel-query-builder</a> whose <strong>?include=product,product.merchant </strong>enables you to get rid of the fractal package.</blockquote><p>Why on steroids? The conditional relationship enables the application to <strong>eagerly load JUST the requested relationships</strong>, namely product and product.merchant only when they are requested.</p><p>Let’s suppose the consumer does not require the product.merchant nested relation</p><pre>example.com/user/101/reviews?include=product</pre><p>Then, the merchant relation is not loaded and the key is missing entirely from the generated output! <strong>How cool is that?</strong></p><pre>example.com/user/101/reviews?include=not-whitelisted.nested-relation</pre><p>Returns status code 400, with the message: Given include(s) `not-whitelisted.nested-relation` are not allowed. Allowed include(s) are `product, product.merchant`</p><p>Another benefit of Laravel’s Resource is that you can structure the output better. No more nested data keys on every included relation</p><p>Spatie’s query builder can also filter, sort and restrict the nested includes (which fractal can’t do). Moreover, the orWhere statement won’t fool the application to display all protected resources.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CWGplZxc81rfMIZyks-R-w.png" /><figcaption>Review, Product and Merchant resources</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*94VfClkJhk5NfSV1YWAfpA.png" /><figcaption>The controller</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OEg-hKFwiahXEc7Stlq9AQ.png" /><figcaption>Spatie query builder + JSON API paginate</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mb4r0WgqAk3LIB1iUJZ3Sw.png" /><figcaption>The final output</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/856/1*H7pnEVsv5p656XhEkpI7yw.png" /><figcaption>Hooray! The queries</figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7ba7936b8b81" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>