<?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 Vatsal A on Medium]]></title>
        <description><![CDATA[Stories by Vatsal A on Medium]]></description>
        <link>https://medium.com/@vatsal.a?source=rss-230af60d145c------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*UsSOs_ERBYDbpb2yQSzabg.jpeg</url>
            <title>Stories by Vatsal A on Medium</title>
            <link>https://medium.com/@vatsal.a?source=rss-230af60d145c------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 03:47:14 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@vatsal.a/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[Getting Started with Laravel API Resources for Clean JSON Responses In Laravel]]></title>
            <link>https://medium.com/addweb-engineering/getting-started-with-laravel-api-resources-for-clean-json-responses-in-laravel-0460084d3cc3?source=rss-230af60d145c------2</link>
            <guid isPermaLink="false">https://medium.com/p/0460084d3cc3</guid>
            <category><![CDATA[laravel]]></category>
            <category><![CDATA[php]]></category>
            <category><![CDATA[clean-code]]></category>
            <category><![CDATA[jason]]></category>
            <category><![CDATA[rest-api]]></category>
            <dc:creator><![CDATA[Vatsal A]]></dc:creator>
            <pubDate>Fri, 30 Jan 2026 07:49:32 GMT</pubDate>
            <atom:updated>2026-01-30T07:50:55.109Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="Clean JSON APIs in Laravel with API Resources" src="https://cdn-images-1.medium.com/max/1024/1*a2Y5V7r8N5Aj5oUGhHEvuw.png" /><figcaption>Clean JSON APIs in Laravel with API Resources</figcaption></figure><blockquote><em>“Clean code always looks like it was written by someone who cares.”<br> — Michael Feathers</em></blockquote><h3>Key Takeaways</h3><ul><li>API Resources format and clean your JSON responses.</li><li>They help separate transformation logic from controller logic.</li><li>Supports nested relationships and conditional fields.</li><li>You can disable default data wrapping.</li><li>Adds meta information like API version easily.</li></ul><h3>Index</h3><ol><li>What is a Resource?</li><li>Creating a Resource</li><li>make() vs collection()</li><li>Loading Relationships with Resources</li><li>Hiding Data with Resources</li><li>Best Practices</li><li>Interesting Facts</li><li>FAQ’s</li><li>Conclusion</li></ol><h3>1. What is a Resource?</h3><p>A <strong>Resource</strong> in Laravel is a way to transform your Eloquent models into a customized array or JSON structure. It allows you to:</p><ul><li>Hide sensitive fields</li><li>Format data (e.g., timestamps)</li><li>Include related models (relationships)</li><li>Maintain consistency in API responses</li></ul><p><strong>Default JSON without resource:</strong></p><pre>{<br>  &quot;id&quot;: 1,<br>  &quot;name&quot;: &quot;John&quot;,<br>  &quot;email&quot;: &quot;john@example.com&quot;,<br>  &quot;email_verified_at&quot;: &quot;2024-01-01&quot;,<br>  &quot;created_at&quot;: &quot;2024-01-01T00:00:00.000000Z&quot;,<br>  &quot;updated_at&quot;: &quot;2024-01-01T00:00:00.000000Z&quot;<br>}</pre><p><strong>With a resource:</strong></p><pre>{<br>  &quot;id&quot;: 1,<br>  &quot;name&quot;: &quot;John&quot;<br>}</pre><h3>2. Creating a Resource</h3><p>Generate a resource with Artisan:</p><pre>php artisan make:resource UserResource</pre><p>It creates a file in:</p><pre>app/Http/Resources/UserResource.php<br>namespace App\Http\Resources;</pre><pre>use Illuminate\Http\Resources\Json\JsonResource;</pre><pre>class UserResource extends JsonResource<br>{<br>    public function toArray($request)<br>    {<br>        return [<br>            &#39;id&#39; =&gt; $this-&gt;id,<br>            &#39;name&#39; =&gt; $this-&gt;name,<br>            &#39;email&#39; =&gt; $this-&gt;email,<br>        ];<br>    }<br>}</pre><p>Use it in a controller:</p><pre>use App\Http\Resources\UserResource;<br>use App\Models\User;</pre><pre>public function show($id)<br>{<br>    return new UserResource(User::findOrFail($id));<br>}</pre><h3>3. make() vs collection()</h3><p><strong>Using </strong><strong>make() for a Single Model</strong><br><strong>Controller:</strong></p><pre>public function show($id)<br>{<br>    $user = User::findOrFail($id);<br>    return UserResource::make($user);<br>}</pre><p><strong>Output:</strong></p><pre>{<br>  &quot;id&quot;: 1,<br>  &quot;name&quot;: &quot;John Doe&quot;,<br>  &quot;email&quot;: &quot;john@example.com&quot;<br>}</pre><p><strong>Using </strong><strong>collection() for Multiple Models</strong><br><strong>Controller:</strong></p><pre>public function index()<br>{<br>    $users = User::all();<br>    return UserResource::collection($users);<br>}</pre><p><strong>Output:</strong></p><pre>[<br>  {<br>    &quot;id&quot;: 1,<br>    &quot;name&quot;: &quot;John Doe&quot;,<br>    &quot;email&quot;: &quot;john@example.com&quot;<br>  },<br>  {<br>    &quot;id&quot;: 2,<br>    &quot;name&quot;: &quot;Jane Doe&quot;,<br>    &quot;email&quot;: &quot;jane@example.com&quot;<br>  }<br>]</pre><p><strong>make()</strong> → single object<br><strong>collection()</strong> → list of objects</p><h3>4. Loading Relationships with Resources</h3><p><strong>Example: User has one Profile</strong><br>User.php</p><pre>public function profile()<br>{<br>    return $this-&gt;hasOne(Profile::class);<br>}</pre><p>Profile.php</p><pre>public function user()<br>{<br>    return $this-&gt;belongsTo(User::class);<br>}</pre><p><strong>Create ProfileResource</strong></p><pre>php artisan make:resource ProfileResource</pre><pre>class ProfileResource extends JsonResource<br>{<br>    public function toArray($request)<br>    {<br>        return [<br>            &#39;bio&#39; =&gt; $this-&gt;bio,<br>            &#39;twitter&#39; =&gt; $this-&gt;twitter,<br>        ];<br>    }<br>}</pre><p><strong>Include Profile in UserResource</strong></p><pre>use App\Http\Resources\ProfileResource;</pre><pre>class UserResource extends JsonResource<br>{<br>    public function toArray($request)<br>    {<br>        return [<br>            &#39;id&#39; =&gt; $this-&gt;id,<br>            &#39;name&#39; =&gt; $this-&gt;name,<br>            &#39;email&#39; =&gt; $this-&gt;email,<br>            &#39;profile&#39; =&gt; new ProfileResource($this-&gt;whenLoaded(&#39;profile&#39;)),<br>        ];<br>    }<br>}</pre><p><strong>Controller:</strong></p><pre>public function show($id)<br>{<br>    $user = User::with(&#39;profile&#39;)-&gt;findOrFail($id);<br>    return UserResource::make($user);<br>}</pre><p><strong>Output:</strong></p><pre>{<br>  &quot;id&quot;: 1,<br>  &quot;name&quot;: &quot;John Doe&quot;,<br>  &quot;email&quot;: &quot;john@example.com&quot;,<br>  &quot;profile&quot;: {<br>    &quot;bio&quot;: &quot;Developer at Example&quot;,<br>    &quot;twitter&quot;: &quot;@johndoe&quot;<br>  }<br>}</pre><p>Without <strong>with(‘profile’)</strong> in controller:</p><pre>{<br>  &quot;id&quot;: 1,<br>  &quot;name&quot;: &quot;John Doe&quot;,<br>  &quot;email&quot;: &quot;john@example.com&quot;,<br>  &quot;profile&quot;: null<br>}</pre><p>Why use <strong>whenLoaded()</strong> ?</p><ul><li>Prevents <strong>unnecessary DB queries</strong> (lazy loading)</li><li>Allows <strong>optional inclusion</strong> of relationships based on controller logic</li></ul><h3>5. Hiding Data with Resources</h3><p>Let’s say we want to hide the user’s password and timestamps:</p><pre>public function toArray($request)<br>{<br>    return [<br>        &#39;id&#39;   =&gt; $this-&gt;id,<br>        &#39;name&#39; =&gt; $this-&gt;name,<br>        // Email is optional<br>        &#39;email&#39; =&gt; $this-&gt;when(request()-&gt;user()-&gt;isAdmin(), $this-&gt;email),<br>    ];<br>}</pre><p>You can also use:</p><ul><li>when()</li><li>mergeWhen()</li><li>whenLoaded() for relationships</li></ul><blockquote><em>“Creativity is thinking up new things. Innovation is doing new things.”<br> — Theodore Levitt</em></blockquote><h3>6. Best Practices</h3><ul><li>Use resources for APIs, not inside Blade views.</li><li>Keep transformations lightweight and readable.</li><li>Use nested resources for relationships.</li><li>Use whenLoaded() to avoid empty/null relationship issues.</li></ul><blockquote><em>“Success is the sum of small efforts, repeated day in and day out.”<br> — Robert Collier</em></blockquote><h3>7. Interesting Facts</h3><ul><li>You can nest as many resource layers as needed. (<a href="https://laravel.com/docs/12.x/eloquent-resources#nested-resources">Laravel API Resources — Nested Resources</a>)</li><li>Can be reused between web and API if structured correctly. (<a href="https://laravel.com/docs/12.x/eloquent-resources">Laravel API Resources Overview</a>)</li><li>Works well with Laravel Pagination for out-of-the-box data/links/meta output. (<a href="https://laravel.com/docs/12.x/eloquent-resources#paginated-responses">Laravel Pagination with Resources</a>)</li></ul><h3>8. FAQ’s</h3><p><strong>Q: Should I use Resources in internal tools?</strong><br>A: Yes, if they’re API-driven. It keeps things consistent.</p><p><strong>Q: Can I return extra metadata?</strong><br>A: Yes:</p><pre>return (new UserResource($user))-&gt;additional([<br>    &#39;meta&#39; =&gt; [&#39;version&#39; =&gt; &#39;1.0.0&#39;]<br>]);</pre><p><strong>Q: Can I use a resource without a model?</strong><br>A: Yes, you can wrap any array of data:</p><pre>return JsonResource::make([<br>    &#39;message&#39; =&gt; &#39;Success&#39;,<br>    &#39;data&#39;    =&gt; [...]<br>]);</pre><h3>9. Conclusion</h3><p>Laravel API Resources provide a powerful and clean way to structure JSON responses in APIs. They make your controllers leaner, your API more consistent, and your data transformation more manageable. Use them to deliver beautiful, reliable API output every time.</p><p>About the Author: <em>Vatsal is a web developer at </em><a href="https://www.addwebsolution.com/our-capabilities/laravel-development-agency"><em>AddWebSolution</em></a><em>. Building web magic with Laravel, PHP, MySQL, Vue.js &amp; more.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=0460084d3cc3" width="1" height="1" alt=""><hr><p><a href="https://medium.com/addweb-engineering/getting-started-with-laravel-api-resources-for-clean-json-responses-in-laravel-0460084d3cc3">Getting Started with Laravel API Resources for Clean JSON Responses In Laravel</a> was originally published in <a href="https://medium.com/addweb-engineering">AddWeb Digital</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[When to Use Gates, Policies, and the Spatie Permission Package in Laravel]]></title>
            <link>https://medium.com/addweb-engineering/when-to-use-gates-policies-and-the-spatie-permission-package-in-laravel-7bd6cd792838?source=rss-230af60d145c------2</link>
            <guid isPermaLink="false">https://medium.com/p/7bd6cd792838</guid>
            <category><![CDATA[rbac]]></category>
            <category><![CDATA[laravel]]></category>
            <category><![CDATA[authorization]]></category>
            <category><![CDATA[spatiepermission]]></category>
            <category><![CDATA[permission]]></category>
            <dc:creator><![CDATA[Vatsal A]]></dc:creator>
            <pubDate>Thu, 13 Nov 2025 12:36:54 GMT</pubDate>
            <atom:updated>2025-11-13T12:38:21.674Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="Laravel roles and permissions illustration." src="https://cdn-images-1.medium.com/max/1024/1*yYFb6-Qc9f-aAyXBAYFT4A.png" /><figcaption>Roles and permissions in Laravel</figcaption></figure><blockquote><em>“Good code is its own best documentation.”<br></em><strong><em> — Steve McConnell</em></strong></blockquote><h3>Key Takeaways</h3><ul><li>Learn what <strong>Gates, Policies,</strong> and <strong>Spatie Permission</strong> are.</li><li>Understand <strong>when</strong> to choose each.</li><li>See <strong>step-by-step examples</strong>.</li><li>Apply everything in <strong>one clear practical scenario</strong>.</li></ul><h3>Index</h3><ol><li>What is Authorization in Laravel?</li><li>When Should You Use Gates?</li><li>When Should You Use Policies?</li><li>When Should You Use Spatie Permission?</li><li>Example Code: Gates</li><li>Example Code: Policies</li><li>Example Code: Spatie Permission</li><li>Practical Example: A Blogging Platform</li><li>Stats</li><li>Interesting Facts</li><li>FAQs</li><li>Conclusion</li></ol><h3>1. What is Authorization in Laravel?</h3><p><strong>Authorization</strong> determines what a user can do — unlike authentication (who the user is).</p><p>Laravel provides:</p><ul><li><strong>Gates</strong> (simple closures to allow/deny actions)</li><li><strong>Policies</strong> (classes that group rules for a model)</li><li>Integration with packages like <strong>Spatie Permission</strong> for advanced role and permission management.</li></ul><h3>2. When Should You Use Gates?</h3><p>Use <strong>Gates</strong> when:</p><ul><li>You need quick, simple checks.</li><li>Your rule doesn’t depend on Eloquent models.</li></ul><p><strong>Example:</strong><br>“Is the user an admin?”<br>“Does the user have a verified email?”</p><h3>3. When Should You Use Policies?</h3><p>Use <strong>Policies</strong> when:</p><ul><li>Your rules are about a <strong>specific model</strong> (like Post, Order, Product).</li><li>You want organized, reusable logic.</li></ul><p><strong>Example:</strong><br>“Does this user own this post?”<br>“Can the user delete this order?”</p><h3>4. When Should You Use Spatie Permission?</h3><p>Use <strong>Spatie Permission</strong> when:</p><ul><li>You want <strong>Role-Based Access Control (RBAC)</strong> or <strong>Permission-Based Access Control</strong>.</li><li>You need to manage roles and permissions dynamically (e.g., via an admin UI).</li><li>Your app has multiple user types (admins, editors, moderators, etc).</li></ul><h3>5. Example Code: Gates</h3><p><strong>How to define a Gate:</strong></p><p>In <strong>AuthServiceProvider.php:</strong></p><pre>use Illuminate\Support\Facades\Gate;</pre><pre>public function boot()<br>{<br>  $this-&gt;registerPolicies();<br>  Gate::define(&#39;access-admin&#39;, function ($user) {<br>    return $user-&gt;is_admin;<br>  });<br>}</pre><p><strong>How to use:</strong></p><p>In Controller:</p><pre>if (Gate::allows(&#39;access-admin&#39;)) {<br>  // Allow<br>} else {<br>  abort(403);<br>}</pre><p>In Blade:</p><pre>@can(&#39;access-admin&#39;)<br>  &lt;a href=&quot;/admin&quot;&gt;Admin Dashboard&lt;/a&gt;<br>@endcan</pre><h3>6. Example Code: Policies</h3><p><strong>Generate a Policy:</strong></p><pre>php artisan make:policy PostPolicy - model=Post</pre><p><strong>Define logic:</strong></p><pre>public function update(User $user, Post $post)<br>{<br>  return $user-&gt;id === $post-&gt;user_id;<br>}</pre><p><strong>How to use:</strong></p><p>In Controller:</p><pre>$this-&gt;authorize(&#39;update&#39;, $post);</pre><p>In Blade:</p><pre>@can(&#39;update&#39;, $post)<br>  &lt;button&gt;Edit Post&lt;/button&gt;<br>@endcan</pre><h3>7. Example Code: Spatie Permission</h3><p><strong>Installation:</strong></p><pre>composer require spatie/laravel-permission</pre><pre>php artisan vendor:publish - provider=&quot;Spatie\Permission\PermissionServiceProvider&quot;</pre><pre>php artisan migrate</pre><p><strong>Assign Roles &amp; Permissions:</strong></p><pre>use Spatie\Permission\Models\Role;<br>use Spatie\Permission\Models\Permission;</pre><pre>$permission = Permission::create([&#39;name&#39; =&gt; &#39;edit articles&#39;]);<br>$role = Role::create([&#39;name&#39; =&gt; &#39;admin&#39;]);<br>$role-&gt;givePermissionTo(&#39;edit articles&#39;);<br>$user-&gt;assignRole(&#39;admin&#39;);</pre><p><strong>Usage:</strong></p><p>In Controller:</p><pre>if ($user-&gt;can(&#39;edit articles&#39;)) {<br>  // Allow<br>}</pre><p>In Blade:</p><pre>@can(&#39;edit articles&#39;)<br>  &lt;button&gt;Edit Article&lt;/button&gt;<br>@endcan</pre><h3>8. Practical Example: A Blogging Platform</h3><p>Let’s see <strong>the same scenario implemented with each method, so you get a side-by-side comparison.</strong></p><p><strong>Scenario</strong><br>Users can create posts.<br>Each post has an author_id.<br>Only the <strong>author or an admin</strong> can <strong>delete</strong> a post.</p><blockquote><em>“Your work is going to fill a large part of your life, so love what you do.”<br></em><strong><em> — Steve Jobs</em></strong></blockquote><p><strong>With Gates</strong><br>Definition:</p><pre>Gate::define(&#39;delete-post&#39;, function ($user, $post) {<br>  return $user-&gt;id === $post-&gt;user_id || $user-&gt;is_admin;<br>});</pre><p>Controller:</p><pre>if (Gate::allows(&#39;delete-post&#39;, $post)) {<br>  $post-&gt;delete();<br>} else {<br>  abort(403);<br>}</pre><p>Blade:</p><pre>@can(&#39;delete-post&#39;, $post)<br>  &lt;button&gt;Delete Post&lt;/button&gt;<br>@endcan</pre><p><strong>With Policies</strong><br>Generate Policy:</p><pre>php artisan make:policy PostPolicy - model=Post</pre><p>Define in Policy:</p><pre>public function delete(User $user, Post $post)<br>{<br>  return $user-&gt;id === $post-&gt;user_id || $user-&gt;is_admin;<br>}</pre><p>Controller:</p><pre>$this-&gt;authorize(&#39;delete&#39;, $post);<br>$post-&gt;delete();</pre><p>Blade:</p><pre>@can(&#39;delete&#39;, $post)<br>  &lt;button&gt;Delete Post&lt;/button&gt;<br>@endcan</pre><p><strong>With Spatie Permission</strong><br>Setup:</p><pre>composer require spatie/laravel-permission</pre><pre>php artisan vendor:publish - provider=&quot;Spatie\Permission\PermissionServiceProvider&quot;</pre><pre>php artisan migrate</pre><p>Assign Role and Permission:</p><pre>$permission = Permission::create([&#39;name&#39; =&gt; &#39;delete posts&#39;]);<br>$role = Role::create([&#39;name&#39; =&gt; &#39;admin&#39;]);<br>$role-&gt;givePermissionTo(&#39;delete posts&#39;);<br>$user-&gt;assignRole(&#39;admin&#39;);</pre><p>Controller:</p><pre>if ($user-&gt;can(&#39;delete posts&#39;)) {<br>  $post-&gt;delete();<br>} else {<br>  abort(403);<br>}</pre><p>Blade:</p><pre>@can(&#39;delete posts&#39;)<br>  &lt;button&gt;Delete Post&lt;/button&gt;<br>@endcan</pre><blockquote><em>“Software innovation, like almost every other kind of innovation, requires the ability to collaborate and share ideas.”<br></em><strong><em> — Bill Gates</em></strong></blockquote><h3>9. Stats</h3><ul><li>Spatie Permission &gt;10 million downloads (<a href="https://packagist.org/packages/spatie/laravel-permission">Spatie Laravel Permission on Packagist</a>)</li><li>Gates and Policies have been part of Laravel since version 5.1, first released in June 2015. (<a href="https://laravel.com/docs/5.1/authorization">Authorization — Laravel 5.1</a>)</li></ul><h3>10. Interesting Facts</h3><ul><li>Gates can be registered anywhere — but best in AuthServiceProvider.(<a href="https://laravel.com/docs/12.x/authorization#gates">Laravel Authorization Docs</a>)</li><li>Policies auto-discovered by Laravel. (<a href="https://laravel.com/docs/12.x/authorization#policy-auto-discovery">Laravel Policy Auto-Discovery</a>)</li><li>Spatie Permission caches permissions (php artisan permission:cache-reset).(<a href="https://spatie.be/docs/laravel-permission/v6/introduction">Spatie Laravel Permission Docs</a>)</li></ul><h3>11. FAQs</h3><p>Q: Can I mix Gates, Policies, and Spatie?<br>A: Yes — many projects use them together.</p><p>Q: Are Policies required for Spatie?<br>A: No — but you can combine them.</p><h3>12. Conclusion</h3><p>Here’s a recap:</p><ul><li><strong>Gates:</strong> Small, quick checks.</li><li><strong>Policies:</strong> Organized model rules.</li><li><strong>Spatie Permission:</strong> Powerful RBAC and permissions.</li></ul><p>#Laravel #LaravelTips #LaravelDevelopers #PHP #WebDevelopment #BackendDevelopment #SpatiePermission #LaravelAuthorization #LaravelPolicies #LaravelGates #CodingTips #FullStackDeveloper #WebAppSecurity #LaravelCommunity #CleanCode #LaravelPackages #SoftwareDevelopment #WebDevelopers #TechBlog</p><p>About the Author: <em>Vatsal is a web developer at </em><a href="https://www.addwebsolution.com/our-capabilities/laravel-development-agency"><em>AddWebSolution</em></a><em>. Building web magic with Laravel, PHP, MySQL, Vue.js &amp; more. Blending code, coffee, and creativity to bring ideas to life.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7bd6cd792838" width="1" height="1" alt=""><hr><p><a href="https://medium.com/addweb-engineering/when-to-use-gates-policies-and-the-spatie-permission-package-in-laravel-7bd6cd792838">When to Use Gates, Policies, and the Spatie Permission Package in Laravel</a> was originally published in <a href="https://medium.com/addweb-engineering">AddWeb Digital</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Laravel File Upload Made Modern with FilePond (Single Image Upload)]]></title>
            <link>https://medium.com/addweb-engineering/laravel-file-upload-made-modern-with-filepond-single-image-upload-301955dfe679?source=rss-230af60d145c------2</link>
            <guid isPermaLink="false">https://medium.com/p/301955dfe679</guid>
            <category><![CDATA[tips-laravel]]></category>
            <category><![CDATA[php]]></category>
            <category><![CDATA[file-upload]]></category>
            <category><![CDATA[laravel-development]]></category>
            <category><![CDATA[laravel]]></category>
            <dc:creator><![CDATA[Vatsal A]]></dc:creator>
            <pubDate>Fri, 04 Jul 2025 05:32:09 GMT</pubDate>
            <atom:updated>2025-07-04T06:16:08.087Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="Illustration of Laravel file upload using FilePond." src="https://cdn-images-1.medium.com/max/1024/1*tUnkBRxMvXbubR94mZI-5g.jpeg" /><figcaption>Illustration of Laravel file upload using FilePond.</figcaption></figure><blockquote>“Continuous improvement is better than delayed perfection.”<br><em> </em><strong><em>— Mark Twain</em></strong></blockquote><h3>Key Takeaways</h3><ul><li>Learn how to integrate FilePond for clean and modern single-image uploads.</li><li>Build an interactive drag-and-drop image uploader in Laravel.</li><li>Add real-time image previews using FilePond’s plugin.</li><li>Handle file uploads securely on the Laravel backend.</li></ul><h3>Index</h3><p>1. Why FilePond for Laravel?<br>2. Laravel Project Setup<br>3. Create Migration<br>4. Model &amp; Controller<br>5. Routes<br>6. FilePond Integration in Blade<br>7. Storage Setup<br>8. Preview Enabled with Plugin<br>9. Final Output<br>10. Interesting Facts<br>11. FAQs<br>12. Conclusion</p><h3>1. Why FilePond for Laravel?</h3><p>Traditional HTML file inputs look outdated and provide no interactivity. FilePond brings:</p><ul><li>Drag-and-drop UX</li><li>Async file upload</li><li>Image preview</li><li>Plugin-based enhancement</li></ul><h3>2. Laravel Project Setup</h3><pre>composer create-project laravel/laravel filepond-demo<br>cd filepond-demo</pre><h3>3. Create Migration</h3><pre>php artisan make:migration create_uploads_table</pre><p>Update migration:</p><pre>Schema::create(&#39;uploads&#39;, function (Blueprint $table) {<br>$table-&gt;id();<br>$table-&gt;string(&#39;filename&#39;);<br>$table-&gt;string(&#39;path&#39;);<br>$table-&gt;timestamps();<br>});</pre><p>Run:</p><pre>php artisan migrate</pre><h3>4. Model &amp; Controller</h3><h4>Model: Upload.php</h4><pre>namespace App\Models;<br>use Illuminate\Database\Eloquent\Model;<br>class Upload extends Model<br>{<br>  protected $fillable = [&#39;filename&#39;, &#39;path&#39;];<br>}</pre><h4>Controller: FileUploadController.php</h4><pre>php artisan make:controller FileUploadController</pre><pre>namespace App\Http\Controllers;<br><br>use Illuminate\Http\Request;<br>use App\Models\Upload;<br>use Illuminate\Support\Facades\Storage;<br><br>class FileUploadController extends Controller<br>{<br>    public function index()<br>    {<br>        return view(&#39;upload&#39;);<br>    }<br><br>    public function store(Request $request)<br>    {<br>        $request-&gt;validate([<br>            &#39;file&#39; =&gt; &#39;required|image|mimes:jpeg,png,jpg|max:2048&#39;,<br>        ]);<br><br>        $file = $request-&gt;file(&#39;file&#39;);<br>        $path = $file-&gt;store(&#39;uploads&#39;, &#39;public&#39;);<br><br>        $upload = Upload::create([<br>            &#39;filename&#39; =&gt; $file-&gt;getClientOriginalName(),<br>            &#39;path&#39; =&gt; $path,<br>        ]);<br><br>        return response()-&gt;json([<br>            &#39;id&#39; =&gt; $upload-&gt;id,<br>            &#39;url&#39; =&gt; Storage::url(https://medium.com/$path),<br>        ]);<br>    }<br>}</pre><h3>5. Routes</h3><pre>use App\Http\Controllers\FileUploadController;<br><br>Route::get(&#39;/&#39;, [FileUploadController::class, &#39;index&#39;]);<br>Route::post(&#39;/upload&#39;, [FileUploadController::class, &#39;store&#39;]);</pre><h3>6. FilePond Integration in Blade</h3><p>Create resources/views/upload.blade.php</p><pre>&lt;!DOCTYPE html&gt;<br>&lt;html lang=&quot;en&quot;&gt;<br>&lt;head&gt;<br>    &lt;meta charset=&quot;UTF-8&quot;&gt;<br>    &lt;title&gt;Laravel Single Image Upload&lt;/title&gt;<br>    &lt;meta name=&quot;csrf-token&quot; content=&quot;{{ csrf_token() }}&quot;&gt;<br><br>    &lt;!-- FilePond Core CSS --&gt;<br>    &lt;link href=&quot;https://unpkg.com/filepond@^4/dist/filepond.min.css&quot; rel=&quot;stylesheet&quot;&gt;<br>    &lt;link href=&quot;https://unpkg.com/filepond-plugin-image-preview/dist/filepond-plugin-image-preview.min.css&quot; rel=&quot;stylesheet&quot;&gt;<br>&lt;/head&gt;<br>&lt;body&gt;<br>    &lt;h2&gt;Upload an Image&lt;/h2&gt;<br>    &lt;input type=&quot;file&quot; class=&quot;filepond&quot; name=&quot;file&quot; /&gt;<br><br>    &lt;!-- FilePond Core JS + Plugin --&gt;<br>    &lt;script src=&quot;https://unpkg.com/filepond@^4/dist/filepond.min.js&quot;&gt;&lt;/script&gt;<br>    &lt;script src=&quot;https://unpkg.com/filepond-plugin-image-preview/dist/filepond-plugin-image-preview.min.js&quot;&gt;&lt;/script&gt;<br><br>    &lt;script&gt;<br>        FilePond.registerPlugin(FilePondPluginImagePreview);<br><br>        FilePond.setOptions({<br>            server: {<br>                url: &#39;/upload&#39;,<br>                process: {<br>                    headers: {<br>                        &#39;X-CSRF-TOKEN&#39;: document.querySelector(&#39;meta[name=&quot;csrf-token&quot;]&#39;).getAttribute(&#39;content&#39;)<br>                    }<br>                }<br>            },<br>            allowImagePreview: true,<br>        });<br><br>        FilePond.create(document.querySelector(&#39;input[type=&quot;file&quot;]&#39;));<br>    &lt;/script&gt;<br>&lt;/body&gt;<br>&lt;/html&gt;</pre><h3>7. Storage Setup</h3><pre>php artisan storage:link</pre><h3>8. Preview Enabled with Plugin</h3><p>The FilePondPluginImagePreview plugin provides real-time previews even before the file is uploaded.</p><p>You can even go further later by adding resize, crop, or validation.</p><blockquote>“Creativity is intelligence having fun.”<br><strong><em> — Albert Einstein</em></strong></blockquote><h3>9. Final Output</h3><ol><li>Drag and drop an image</li><li>Preview is shown</li><li>File is uploaded via AJAX to Laravel</li><li>Stored in storage/app/public/uploads</li><li>URL returned in response</li></ol><blockquote>“Any fool can write code that a computer can understand. Good programmers write code that humans can understand.”<br><strong><em> — Martin Fowler</em></strong></blockquote><h3>10. Interesting Facts</h3><ul><li>FilePond compresses images up to <strong>6×</strong> smaller before upload by resizing and transforming them client-side with dedicated plugins. (<a href="https://pqina.nl/filepond/docs/patterns/plugins/image-resize/">FilePond Docs — Image Resize</a>, <a href="https://pqina.nl/filepond/docs/patterns/plugins/image-transform/">FilePond Docs — Image Transform</a>)</li><li>Plugin architecture makes it easy to add/remove functionality.(<a href="https://pqina.nl/filepond/docs/patterns/plugins/">FilePond Docs — Plugin Architecture</a>)</li><li>PQINA, creators of FilePond, also built <strong>Pintura</strong> — used by major companies like Samsung &amp; Amazon.(<a href="https://pqina.nl/">PQINA Official Website</a>, <a href="https://pqina.nl/pintura/">Pintura Image Editor</a>, <a href="https://pqina.nl/pintura/#companies">Companies Using Pintura</a>)</li></ul><h3>11. FAQs</h3><p><strong>Q: Is this upload async?<br></strong> Yes, FilePond uses AJAX to send the image instantly after selecting.</p><p><strong>Q: Can I show uploaded image after upload?<br></strong> Yes, You can use the url from the JSON response and show it dynamically.</p><p><strong>Q: Is CSRF handled?<br></strong> Yes, the token is sent in headers via JavaScript.</p><p><strong>Q: What happens if validation fails?<br></strong> Laravel sends back a 422 error, and FilePond will automatically display it.</p><h3>12. Conclusion</h3><p>FilePond provides an elegant, modern way to handle image uploads in Laravel. With real-time previews and automatic AJAX uploads, it vastly improves UX and developer productivity.</p><p>In the next part, we’ll cover <strong>multiple uploads, delete support, drag-and-drop reordering, and plugins like resize and crop.</strong></p><p>#Laravel #LaravelTips #LaravelDevelopment #PHP #FileUpload #FilePond #ModernFileUpload #ImageUpload #WebDevelopment #WebDev #UserExperience #FrontendDevelopment #BackendDevelopment #SmartWork #Productivity #CleanCode #OpenSource #JavaScript #TechTutorial #Coding</p><p>About the Author: <em>Vatsal is a web developer at </em><a href="https://www.addwebsolution.com/"><strong>AddWebSolution</strong></a>.<em> Building web magic with Laravel, PHP, MySQL, Vue.js &amp; more. Blending code, coffee, and creativity to bring ideas to life.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=301955dfe679" width="1" height="1" alt=""><hr><p><a href="https://medium.com/addweb-engineering/laravel-file-upload-made-modern-with-filepond-single-image-upload-301955dfe679">Laravel File Upload Made Modern with FilePond (Single Image Upload)</a> was originally published in <a href="https://medium.com/addweb-engineering">AddWeb Digital</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>