<?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: Aubin Boris Simo Tsebo</title>
    <description>The latest articles on DEV Community by Aubin Boris Simo Tsebo (@aubin_borissimotsebo).</description>
    <link>https://dev.to/aubin_borissimotsebo</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4216921%2F8a341a61-9d96-46bc-b75f-5b3613df5743.jpeg</url>
      <title>DEV Community: Aubin Boris Simo Tsebo</title>
      <link>https://dev.to/aubin_borissimotsebo</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/aubin_borissimotsebo"/>
    <language>en</language>
    <item>
      <title>Understanding Idempotency: How to Prevent Duplicate Operations in Software Systems</title>
      <dc:creator>Aubin Boris Simo Tsebo</dc:creator>
      <pubDate>Wed, 11 Nov 2026 08:19:25 +0000</pubDate>
      <link>https://dev.to/aubin_borissimotsebo/understanding-idempotency-how-to-prevent-duplicate-operations-in-software-systems-4ao7</link>
      <guid>https://dev.to/aubin_borissimotsebo/understanding-idempotency-how-to-prevent-duplicate-operations-in-software-systems-4ao7</guid>
      <description>&lt;p&gt;&lt;strong&gt;Understanding Idempotency: How to Prevent Duplicate Operations in Software Systems&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;_Introduction&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F4l26fqaano1fus6ktr1v.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F4l26fqaano1fus6ktr1v.jpg" alt=" " width="800" height="447"&gt;&lt;/a&gt;_&lt;/p&gt;

&lt;p&gt;Imagine a user making an online payment of $100.&lt;/p&gt;

&lt;p&gt;They click the Pay button once, but their internet connection is unstable. Nothing seems to happen, so they click again. Then a third time.&lt;/p&gt;

&lt;p&gt;A few seconds later, they discover that their account has been charged three times.&lt;/p&gt;

&lt;p&gt;What went wrong?&lt;/p&gt;

&lt;p&gt;The problem is not necessarily the payment system itself. It may be that the application does not properly handle repeated requests.&lt;/p&gt;

&lt;p&gt;This is where idempotency becomes essential.&lt;/p&gt;

&lt;p&gt;Idempotency is a fundamental concept in software engineering, particularly when building REST APIs, payment systems, distributed applications, and services that must remain reliable even when requests are repeated.&lt;/p&gt;

&lt;p&gt;In this article, we will explore what idempotency means, how it works internally, why it matters, and how to implement it in practice.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;What Is Idempotency?&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Idempotency is a property of an operation that ensures executing it multiple times produces the same final state as executing it once.&lt;/p&gt;

&lt;p&gt;In simpler terms:&lt;/p&gt;

&lt;p&gt;Repeating the same operation should not produce additional effects after the first successful execution.&lt;/p&gt;

&lt;p&gt;Consider a mathematical example:&lt;/p&gt;

&lt;p&gt;f(x) = x&lt;/p&gt;

&lt;p&gt;f(f(x)) = x&lt;/p&gt;

&lt;p&gt;Applying the operation again does not change the result.&lt;/p&gt;

&lt;p&gt;In software engineering, the principle is similar, although we are often dealing with database records, financial transactions, API requests, and external services rather than mathematical values.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Updating a user's address to the same value several times should leave the same final address.&lt;/li&gt;
&lt;li&gt;Deleting an already deleted resource should not create additional effects.&lt;/li&gt;
&lt;li&gt;Repeating a payment request should not charge the customer multiple times.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;However, idempotency does not mean that an operation is executed only once. It means that repeated executions do not change the final outcome beyond the first execution.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Why Is Idempotency Important?&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Modern applications communicate through networks.&lt;/p&gt;

&lt;p&gt;Unfortunately, network communication is not always reliable.&lt;/p&gt;

&lt;p&gt;Several situations can cause the same request to reach a server more than once:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A user clicks a button multiple times.&lt;/li&gt;
&lt;li&gt;A mobile application automatically retries a failed request.&lt;/li&gt;
&lt;li&gt;A network timeout occurs after the server has already processed the request.&lt;/li&gt;
&lt;li&gt;A client does not receive the response and assumes the operation failed.&lt;/li&gt;
&lt;li&gt;A message broker delivers the same message more than once.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Consider this scenario:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A client sends a payment request.&lt;/li&gt;
&lt;li&gt;The server processes the payment successfully.&lt;/li&gt;
&lt;li&gt;The response gets lost because of a network problem.&lt;/li&gt;
&lt;li&gt;The client sends the same request again.&lt;/li&gt;
&lt;li&gt;The server processes another payment.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The client cannot always know whether the first request was successfully processed.&lt;/p&gt;

&lt;p&gt;Without an appropriate mechanism, duplicate operations can lead to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Duplicate financial transactions.&lt;/li&gt;
&lt;li&gt;Multiple orders for the same purchase.&lt;/li&gt;
&lt;li&gt;Repeated account creation.&lt;/li&gt;
&lt;li&gt;Duplicate database records.&lt;/li&gt;
&lt;li&gt;Inconsistent business data.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Idempotency helps protect applications against these situations.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;How Does Idempotency Work Internally?&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Idempotency is not a built-in method or function in PHP, JavaScript, Python, or another programming language.&lt;/p&gt;

&lt;p&gt;It is a software design principle that developers implement using different mechanisms.&lt;/p&gt;

&lt;p&gt;One common approach is the Idempotency Key.&lt;/p&gt;

&lt;p&gt;An idempotency key is a unique identifier associated with a particular logical operation.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;POST /api/payments&lt;br&gt;
Content-Type: application/json&lt;br&gt;
Idempotency-Key: payment-12345&lt;/p&gt;

&lt;p&gt;The server uses this key to recognize whether the same operation has already been processed.&lt;/p&gt;

&lt;p&gt;The general workflow is:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The client generates an idempotency key.&lt;/li&gt;
&lt;li&gt;The client sends the key with the request.&lt;/li&gt;
&lt;li&gt;The server checks whether the key already exists.&lt;/li&gt;
&lt;li&gt;If the key is new, the server processes the operation and stores its result.&lt;/li&gt;
&lt;li&gt;If the key already exists, the server returns the previously stored result instead of repeating the operation.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Conceptually:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;              CLIENT
                 |
         Send payment request
                 |
          Idempotency Key
                 |
                 v
               SERVER
                 |
          Check the key
                 |
          +------+------+
          |             |
       New key       Existing key
          |             |
          v             v
   Process payment   Return stored
   Store result      result
          |             |
          +------+------+
                 |
                 v
           CLIENT RESPONSE
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The key is important because the server needs a reliable way to identify repeated attempts belonging to the same logical operation.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Practical Implementation with PHP and Laravel&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's consider a simplified payment system built with Laravel.&lt;/p&gt;

&lt;p&gt;Step 1: Create an idempotency key column&lt;/p&gt;

&lt;p&gt;We need to store the key associated with each payment.&lt;/p&gt;

&lt;p&gt;A migration could look like this:&lt;/p&gt;

&lt;p&gt;Schema::table('payments', function (Blueprint $table) {&lt;br&gt;
    $table-&amp;gt;string('idempotency_key')-&amp;gt;unique();&lt;br&gt;
});&lt;/p&gt;

&lt;p&gt;The unique constraint is important.&lt;/p&gt;

&lt;p&gt;It provides database-level protection against two concurrent requests inserting the same key.&lt;/p&gt;

&lt;p&gt;Step 2: Send the idempotency key from the client&lt;/p&gt;

&lt;p&gt;For example, using JavaScript and Axios:&lt;/p&gt;

&lt;p&gt;const idempotencyKey = crypto.randomUUID();&lt;/p&gt;

&lt;p&gt;await axios.post('/api/payments', {&lt;br&gt;
    amount: 10000&lt;br&gt;
}, {&lt;br&gt;
    headers: {&lt;br&gt;
        'Idempotency-Key': idempotencyKey&lt;br&gt;
    }&lt;br&gt;
});&lt;/p&gt;

&lt;p&gt;An important detail: the same key must be reused when retrying the same logical operation.&lt;/p&gt;

&lt;p&gt;Generating a new key for every retry defeats the purpose of idempotency.&lt;/p&gt;

&lt;p&gt;Step 3: Check the key on the server&lt;/p&gt;

&lt;p&gt;Here is a simplified Laravel example:&lt;/p&gt;

&lt;p&gt;public function store(Request $request)&lt;br&gt;
{&lt;br&gt;
    $key = $request-&amp;gt;header('Idempotency-Key');&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$existingPayment = Payment::where(
    'idempotency_key',
    $key
)-&amp;gt;first();

if ($existingPayment) {
    return response()-&amp;gt;json([
        'message' =&amp;gt; 'Payment already processed',
        'payment' =&amp;gt; $existingPayment
    ]);
}

$payment = Payment::create([
    'user_id' =&amp;gt; auth()-&amp;gt;id(),
    'amount' =&amp;gt; $request-&amp;gt;amount,
    'status' =&amp;gt; 'success',
    'idempotency_key' =&amp;gt; $key,
]);

return response()-&amp;gt;json([
    'message' =&amp;gt; 'Payment successful',
    'payment' =&amp;gt; $payment
], 201);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;}&lt;/p&gt;

&lt;p&gt;This example illustrates the basic principle, but it is not sufficient for a production payment system.&lt;/p&gt;

&lt;p&gt;In particular, checking the key and creating the payment are separate operations. Two requests arriving simultaneously could both pass the initial check.&lt;/p&gt;

&lt;p&gt;A production implementation must handle concurrency using database transactions, unique constraints, and appropriate conflict handling.&lt;/p&gt;

&lt;p&gt;It should also validate the request, associate the key with the authenticated user and operation, and verify that a repeated key is not being reused with different payment details.&lt;/p&gt;

&lt;p&gt;For real payment providers, the external payment operation must also be protected against duplicate execution.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;What Happens When a User Clicks Three Times?&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's return to our payment example.&lt;/p&gt;

&lt;p&gt;The user wants to pay 10,000 FCFA and accidentally sends the same request three times.&lt;/p&gt;

&lt;p&gt;All three requests use the same idempotency key.&lt;/p&gt;

&lt;p&gt;Request| Key| Server behavior&lt;br&gt;
First request| payment-12345| Processes the payment&lt;br&gt;
Second request| payment-12345| Returns the existing result&lt;br&gt;
Third request| payment-12345| Returns the existing result&lt;/p&gt;

&lt;p&gt;The result:&lt;/p&gt;

&lt;p&gt;One logical payment instead of three.&lt;/p&gt;

&lt;p&gt;This is the main advantage of idempotency.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Idempotency and HTTP Methods&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Idempotency is also an important concept in REST API design.&lt;/p&gt;

&lt;p&gt;According to HTTP semantics, certain HTTP methods are defined as idempotent.&lt;/p&gt;

&lt;p&gt;HTTP method| Idempotent?| Explanation&lt;br&gt;
GET| Yes| Retrieves a resource without changing its state.&lt;br&gt;
PUT| Yes| Replaces a resource with the same representation.&lt;br&gt;
DELETE| Yes| Repeated deletion does not change the final state.&lt;br&gt;
POST| Generally no| Can create multiple resources.&lt;br&gt;
PATCH| Not necessarily| Depends on the operation being performed.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;p&gt;PUT /api/users/15&lt;/p&gt;

&lt;p&gt;Sending the same update several times should leave the user with the same information.&lt;/p&gt;

&lt;p&gt;However:&lt;/p&gt;

&lt;p&gt;POST /api/orders&lt;/p&gt;

&lt;p&gt;Sending the same request multiple times can create several orders unless the application implements additional protection.&lt;/p&gt;

&lt;p&gt;It is important to understand that HTTP idempotency concerns the intended effect on the server's state, not necessarily identical responses for every request.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Idempotency vs. Debouncing and Throttling&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;These concepts are sometimes confused because they can all help deal with repeated user actions.&lt;/p&gt;

&lt;p&gt;However, they solve different problems.&lt;/p&gt;

&lt;p&gt;Idempotency&lt;/p&gt;

&lt;p&gt;Ensures that repeating the same logical operation does not produce additional effects.&lt;/p&gt;

&lt;p&gt;Debouncing&lt;/p&gt;

&lt;p&gt;Delays an operation until a period of inactivity has passed.&lt;/p&gt;

&lt;p&gt;For example, a search field can wait until the user stops typing before sending a request.&lt;/p&gt;

&lt;p&gt;Throttling&lt;/p&gt;

&lt;p&gt;Limits how frequently an operation can be executed within a given period.&lt;/p&gt;

&lt;p&gt;For example, an API might accept a maximum of 10 requests per minute.&lt;/p&gt;

&lt;p&gt;These mechanisms are complementary, but they are not interchangeable.&lt;/p&gt;

&lt;p&gt;Disabling a button, using debouncing, or applying throttling does not guarantee idempotency.&lt;/p&gt;

&lt;p&gt;A request can still be repeated through another client, a network retry, or a direct API call.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Where Is Idempotency Used in Real-World Systems?&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Idempotency is particularly useful in:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Payment systems: Preventing duplicate charges.&lt;/li&gt;
&lt;li&gt;E-commerce applications: Avoiding duplicate orders.&lt;/li&gt;
&lt;li&gt;REST APIs: Handling repeated client requests safely.&lt;/li&gt;
&lt;li&gt;Distributed systems: Managing repeated message delivery.&lt;/li&gt;
&lt;li&gt;Background jobs: Preventing duplicate business operations when jobs are retried.&lt;/li&gt;
&lt;li&gt;Banking applications: Protecting sensitive financial operations.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In distributed architectures, messages can be delivered more than once. Therefore, consumers often need to implement idempotent processing to avoid repeating business effects.&lt;/p&gt;

&lt;p&gt;This is especially important because exactly-once execution across distributed systems is difficult to guarantee. In practice, systems often combine at-least-once delivery with idempotent processing.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Best Practices When Implementing Idempotency&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Here are some important principles to follow:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Generate a unique key for each logical operation.&lt;/li&gt;
&lt;li&gt;Reuse the same key when retrying that operation.&lt;/li&gt;
&lt;li&gt;Store the key and the result of the operation.&lt;/li&gt;
&lt;li&gt;Enforce uniqueness at the database level.&lt;/li&gt;
&lt;li&gt;Handle simultaneous requests correctly.&lt;/li&gt;
&lt;li&gt;Associate keys with the appropriate user and operation.&lt;/li&gt;
&lt;li&gt;Validate that repeated requests contain equivalent parameters.&lt;/li&gt;
&lt;li&gt;Define how long idempotency records should be retained.&lt;/li&gt;
&lt;li&gt;Handle failures and pending operations explicitly.&lt;/li&gt;
&lt;li&gt;Never rely exclusively on frontend protection for financial operations.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;A reliable implementation must consider both application logic and database consistency.&lt;/p&gt;

&lt;p&gt;Conclusion&lt;/p&gt;

&lt;p&gt;Idempotency is much more than preventing users from clicking a button multiple times.&lt;/p&gt;

&lt;p&gt;It is an important software engineering principle that helps applications remain reliable when requests are repeated, networks fail, and distributed services communicate unpredictably.&lt;/p&gt;

&lt;p&gt;Understanding idempotency means understanding that in modern software systems, a request can be received more than once, and our applications must be designed to handle that reality.&lt;/p&gt;

&lt;p&gt;A reliable system is not simply one that works when everything goes right. It is one that behaves correctly when things go wrong.&lt;/p&gt;

&lt;p&gt;Have you implemented idempotency in one of your projects? What approach did you use?&lt;/p&gt;

&lt;p&gt;I'd be interested in hearing your experience in the comments.&lt;/p&gt;




&lt;p&gt;© 2026 Aubin Boris Simo. All rights reserved.&lt;/p&gt;

&lt;p&gt;Software Engineer | Software Development &amp;amp; Engineering&lt;/p&gt;

</description>
      <category>idempotency</category>
      <category>backend</category>
      <category>laravel</category>
      <category>software</category>
    </item>
    <item>
      <title>Laravel API Resources: How to Build Clean, Consistent, and Maintainable API Responses</title>
      <dc:creator>Aubin Boris Simo Tsebo</dc:creator>
      <pubDate>Fri, 30 Oct 2026 10:03:46 +0000</pubDate>
      <link>https://dev.to/aubin_borissimotsebo/laravel-api-resources-how-to-build-clean-consistent-and-maintainable-api-responses-3fp3</link>
      <guid>https://dev.to/aubin_borissimotsebo/laravel-api-resources-how-to-build-clean-consistent-and-maintainable-api-responses-3fp3</guid>
      <description>&lt;p&gt;When building an API with Laravel, retrieving data from the database is usually straightforward.&lt;/p&gt;

&lt;p&gt;You create a model, write a query, return the result, and Laravel converts it into JSON.&lt;/p&gt;

&lt;p&gt;But there is an important question that developers sometimes overlook:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Should your API expose your database models exactly as they are?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Imagine you are building a blog API. Your &lt;code&gt;Article&lt;/code&gt; model contains several attributes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;id&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;title&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;slug&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;content&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;is_published&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;created_at&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;updated_at&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Your frontend only needs the article's ID, title, slug, and publication date.&lt;/p&gt;

&lt;p&gt;Should you return every database attribute anyway?&lt;/p&gt;

&lt;p&gt;What happens when you later change your database structure? What if you need to include the author's information? What if different clients expect different response formats?&lt;/p&gt;

&lt;p&gt;This is where &lt;strong&gt;Laravel API Resources&lt;/strong&gt; become extremely useful.&lt;/p&gt;

&lt;p&gt;In this article, we will explore what API Resources are, why they matter, and how to use them in a practical Laravel application.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. The Problem: Returning Eloquent Models Directly
&lt;/h2&gt;

&lt;p&gt;Consider a simple controller:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;App\Models\Article&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ArticleController&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Controller&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;Article&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;all&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This works. Laravel automatically converts the Eloquent collection into a JSON response.&lt;/p&gt;

&lt;p&gt;However, as your application grows, this approach can become difficult to manage.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You have limited control over the attributes exposed by the API.&lt;/li&gt;
&lt;li&gt;Your response structure depends heavily on your database model.&lt;/li&gt;
&lt;li&gt;You may unintentionally expose information that clients do not need.&lt;/li&gt;
&lt;li&gt;Maintaining consistent responses across different endpoints becomes harder.&lt;/li&gt;
&lt;li&gt;Adding relationships can make your responses increasingly complex.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The problem is not that returning Eloquent models is always wrong.&lt;/p&gt;

&lt;p&gt;The problem is that your API response should be designed intentionally.&lt;/p&gt;

&lt;p&gt;Your database structure and your public API structure do not necessarily have to be identical.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. What Are Laravel API Resources?
&lt;/h2&gt;

&lt;p&gt;Laravel API Resources provide a transformation layer between your Eloquent models and the JSON responses returned by your API.&lt;/p&gt;

&lt;p&gt;In simple terms, they allow you to decide:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Which attributes should be returned.&lt;/li&gt;
&lt;li&gt;How those attributes should be named.&lt;/li&gt;
&lt;li&gt;Which relationships should be included.&lt;/li&gt;
&lt;li&gt;How individual models and collections should be represented.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Think of an API Resource as a filter and presentation layer for your API data.&lt;/p&gt;

&lt;p&gt;Instead of allowing your model to determine the entire response, you explicitly define what the client receives.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Creating Your First API Resource
&lt;/h2&gt;

&lt;p&gt;Let's assume we already have an &lt;code&gt;Article&lt;/code&gt; model.&lt;/p&gt;

&lt;p&gt;Laravel provides an Artisan command to generate a resource:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;php artisan make:resource ArticleResource
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By default, Laravel creates the resource inside:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app/Http/Resources/ArticleResource.php
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Open the generated file and define the attributes you want to expose.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="kn"&gt;namespace&lt;/span&gt; &lt;span class="nn"&gt;App\Http\Resources&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Illuminate\Http\Request&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Illuminate\Http\Resources\Json\JsonResource&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ArticleResource&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;JsonResource&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;toArray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Request&lt;/span&gt; &lt;span class="nv"&gt;$request&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kt"&gt;array&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
            &lt;span class="s1"&gt;'id'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="s1"&gt;'title'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="s1"&gt;'slug'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;slug&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="s1"&gt;'content'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="s1"&gt;'published_at'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;created_at&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice something important:&lt;/p&gt;

&lt;p&gt;We are not returning every attribute from the database.&lt;/p&gt;

&lt;p&gt;We are explicitly defining the API response.&lt;/p&gt;

&lt;p&gt;This gives us better control over what our application exposes.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Using the Resource in a Controller
&lt;/h2&gt;

&lt;p&gt;Creating a resource is only the first step. We must also use it when returning data.&lt;/p&gt;

&lt;p&gt;Let's update our controller.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;For a single article:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;App\Http\Resources\ArticleResource&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;App\Models\Article&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;show&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Article&lt;/span&gt; &lt;span class="nv"&gt;$article&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ArticleResource&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$article&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;For a collection of articles:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nv"&gt;$articles&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Article&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;all&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;ArticleResource&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;collection&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$articles&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;collection()&lt;/code&gt; method allows Laravel to transform each article using the same resource definition.&lt;/p&gt;

&lt;p&gt;Now, both endpoints follow a consistent response structure.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Understanding Resource Collections and Pagination
&lt;/h2&gt;

&lt;p&gt;One of the most useful things about API Resources is how naturally they work with pagination.&lt;/p&gt;

&lt;p&gt;Imagine your application has thousands of articles.&lt;/p&gt;

&lt;p&gt;Returning all of them in one response is usually unnecessary and can affect performance.&lt;/p&gt;

&lt;p&gt;Instead, use Laravel's pagination:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nv"&gt;$articles&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Article&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;latest&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;paginate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;ArticleResource&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;collection&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$articles&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Laravel automatically provides pagination information alongside the transformed data.&lt;/p&gt;

&lt;p&gt;A simplified response looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"data"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"title"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Understanding Laravel"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"slug"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"understanding-laravel"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"content"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"An introduction to Laravel..."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"published_at"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"2026-10-30T10:00:00.000000Z"&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"links"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"first"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"..."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"last"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"..."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"prev"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"next"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"..."&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;},&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"meta"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"current_page"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"last_page"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"per_page"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="nl"&gt;"total"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;52&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is particularly useful when building APIs consumed by Vue.js, React, mobile applications, or other clients.&lt;/p&gt;

&lt;p&gt;The frontend can display the articles and use the pagination metadata to navigate between pages.&lt;/p&gt;

&lt;p&gt;You do not have to manually build all this pagination information.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. Working with Relationships
&lt;/h2&gt;

&lt;p&gt;Real-world applications rarely contain isolated models.&lt;/p&gt;

&lt;p&gt;An article may belong to a user, have multiple comments, and belong to a category.&lt;/p&gt;

&lt;p&gt;Let's say we want to include the author of each article in our API response.&lt;/p&gt;

&lt;p&gt;First, define the relationship in your &lt;code&gt;Article&lt;/code&gt; model:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;author&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;belongsTo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;class&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'user_id'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, create a &lt;code&gt;UserResource&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;php artisan make:resource UserResource
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Inside &lt;code&gt;UserResource.php&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UserResource&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;JsonResource&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;toArray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Request&lt;/span&gt; &lt;span class="nv"&gt;$request&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kt"&gt;array&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
            &lt;span class="s1"&gt;'id'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="s1"&gt;'name'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, update &lt;code&gt;ArticleResource&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;App\Http\Resources\UserResource&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;toArray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Request&lt;/span&gt; &lt;span class="nv"&gt;$request&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kt"&gt;array&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="s1"&gt;'id'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s1"&gt;'title'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s1"&gt;'slug'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;slug&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s1"&gt;'content'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;

        &lt;span class="s1"&gt;'author'&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;UserResource&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;whenLoaded&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'author'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;whenLoaded()&lt;/code&gt; method is particularly important.&lt;/p&gt;

&lt;p&gt;It tells Laravel to include the relationship only when it has already been loaded.&lt;/p&gt;

&lt;p&gt;In your controller, you can explicitly load it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nv"&gt;$articles&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Article&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;with&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'author'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;latest&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;paginate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;ArticleResource&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;collection&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$articles&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This approach helps you control relationship loading and avoid unnecessary database queries.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Important:&lt;/strong&gt; &lt;code&gt;whenLoaded()&lt;/code&gt; does not automatically solve the N+1 query problem. You still need to load the relationships appropriately, for example, with &lt;code&gt;with()&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  7. Why Should You Use API Resources?
&lt;/h2&gt;

&lt;p&gt;Let's summarize the practical benefits.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Control over exposed data&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You decide exactly which attributes your API returns. This helps prevent unnecessary information from being exposed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Consistent API responses&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Different endpoints can follow a predictable structure, making frontend integration easier.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Better separation of responsibilities&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Your models handle data and relationships, while resources handle the representation of that data in API responses.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Easier maintenance&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Changes to your database structure do not necessarily require changes to the public API response.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. Relationship management&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You can control how related models are represented and when they appear in responses.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;6. Pagination support&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Resources work naturally with Laravel's pagination and provide useful metadata and navigation links.&lt;/p&gt;

&lt;h2&gt;
  
  
  8. A Few Best Practices
&lt;/h2&gt;

&lt;p&gt;When working with API Resources, keep these points in mind:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Do not expose every database column automatically.&lt;/li&gt;
&lt;li&gt;Create dedicated resources for related models when appropriate.&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;whenLoaded()&lt;/code&gt; for conditional relationships.&lt;/li&gt;
&lt;li&gt;Eager-load relationships in your queries when you need them.&lt;/li&gt;
&lt;li&gt;Use pagination for large collections.&lt;/li&gt;
&lt;li&gt;Keep your response structure consistent.&lt;/li&gt;
&lt;li&gt;Remember that API Resources do not replace authentication, authorization, or request validation.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The objective is not to create a resource class for every possible situation without thinking.&lt;/p&gt;

&lt;p&gt;It is to use resources where they make your API clearer and easier to maintain.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Building an API is not just about retrieving information from a database and returning JSON.&lt;/p&gt;

&lt;p&gt;It is also about deciding how that information should be presented, structured, and consumed by other applications.&lt;/p&gt;

&lt;p&gt;Laravel API Resources provide a clean and practical way to achieve this.&lt;/p&gt;

&lt;p&gt;They help developers build APIs that are easier to understand, integrate, and maintain as applications evolve.&lt;/p&gt;

&lt;p&gt;If you are building APIs with Laravel and you are not using Resources yet, consider introducing them into your next project.&lt;/p&gt;

&lt;p&gt;You may discover that a small architectural decision can make a significant difference in the long-term quality of your application.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Good APIs do not just return data. They return data with structure, consistency, and purpose.&lt;/strong&gt;&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;What about you?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Do you use Laravel API Resources in your projects, or do you prefer returning Eloquent models directly?&lt;/p&gt;

&lt;p&gt;I'd be interested in hearing about your approach and the reasons behind it.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Written by **Boris Aubin Simo&lt;/em&gt;* | Software Engineer*&lt;/p&gt;

&lt;p&gt;Interested in software engineering, Laravel, application architecture, APIs, and digital innovation.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>backend</category>
      <category>laravel</category>
    </item>
  </channel>
</rss>
