<?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: Savio Resende</title>
    <description>The latest articles on DEV Community by Savio Resende (@savioresende).</description>
    <link>https://dev.to/savioresende</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.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2806197%2Fb37f1ce8-f2b5-4267-91ee-dab57dc8d6ca.jpg</url>
      <title>DEV Community: Savio Resende</title>
      <link>https://dev.to/savioresende</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/savioresende"/>
    <language>en</language>
    <item>
      <title>One System, Many Possibilities - Exploring Monoliths, Events, and LLMs</title>
      <dc:creator>Savio Resende</dc:creator>
      <pubDate>Mon, 12 May 2025 00:15:05 +0000</pubDate>
      <link>https://dev.to/savioresende/one-system-many-possibilities-exploring-monoliths-events-and-llms-54ob</link>
      <guid>https://dev.to/savioresende/one-system-many-possibilities-exploring-monoliths-events-and-llms-54ob</guid>
      <description>&lt;p&gt;Monolithic systems can be great for small-scale or early-stage projects. They’re straightforward to build from the ground up, often scale adequately with initial needs, and benefit from a wealth of established solutions. In today’s fast-moving digital world, businesses rely on software to stay competitive, deliver great customer experiences, and adapt to change. However, as systems grow, many organizations find themselves held back by these same monolithic architectures, which become large, complex, and difficult to update. For many different reasons, monolithic systems lead to these issues. Monolithic systems, where all functions are packed into a single codebase, create bottlenecks that slow innovation and frustrate teams. As someone who’s worked with these challenges, I’ve seen firsthand how they can stifle progress, and I’ve come to believe that modern approaches like microservices and event-driven architectures (EDA) are the way forward.&lt;/p&gt;

&lt;p&gt;This article, inspired by an insightful article (&lt;a href="https://seanfalconer.medium.com/why-googles-agent2agent-protocol-needs-apache-kafka-507b1ec456a6" rel="noopener noreferrer"&gt;https://seanfalconer.medium.com/why-googles-agent2agent-protocol-needs-apache-kafka-507b1ec456a6&lt;/a&gt;) shared by my friend &lt;a href="https://x.com/MikeBirdTech" rel="noopener noreferrer"&gt;Mike Bird&lt;/a&gt;, examines the limitations of monolithic systems, the advantages of microservices, and the transformative potential of event-driven architectures. We’ll also explore their relevance to artificial intelligence (AI), particularly through Google’s Agent2Agent (A2A) protocol, an open standard that enables secure, interoperable communication between AI agents using HTTP and JSON-RPC.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Problem with Monolithic Systems
&lt;/h2&gt;

&lt;p&gt;Monolithic systems are like giant, all-in-one machines where every part—user interface, business logic, and data access—is tightly woven together. This setup, known as monolithic architecture, was once the standard in software development because it’s straightforward to build and deploy initially. However, as systems grow, their complexity becomes problematic. There are a few challenges to it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Slow Updates&lt;/strong&gt;: Even a tiny change, like fixing a button on a website, requires rebundling and redeploying the entire application. This process is time-consuming and prone to errors, delaying new features or bug fixes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Tangled Code&lt;/strong&gt;: Monoliths often lead to “spaghetti code,” where dependencies between components are so intertwined that modifying one part risks breaking others. This makes maintenance a nightmare.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Scaling Issues&lt;/strong&gt;: If one function, like payment processing, gets heavy traffic, the whole system must be scaled up, wasting resources on less busy parts.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Innovation Struggles&lt;/strong&gt;: Businesses struggle to integrate new technologies or respond to market changes because the system is rigid and hard to adapt.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example, imagine a retail company using a monolithic e-commerce platform. Adding a new payment method, like cryptocurrency, could take months because the payment module is tightly coupled with inventory management, user authentication, and checkout processes. Developers must update the entire codebase, test every interconnected feature, and redeploy the whole system—delaying the rollout and potentially losing customers to competitors with faster, more modular platforms.&lt;/p&gt;

&lt;p&gt;I’ve felt this frustration myself when working with large systems that resist change. It’s like trying to renovate a house where moving one wall risks collapsing the entire structure. Businesses today can’t afford this kind of inflexibility—they need software that evolves as quickly as their markets do.&lt;/p&gt;

&lt;h3&gt;
  
  
  Bringing this concept to A2A
&lt;/h3&gt;

&lt;p&gt;The limitations of monolithic systems, such as slow updates, tangled code, and scaling inefficiencies, mirror some of the challenges faced by Google’s Agent2Agent (A2A) protocol in its current form. A2A, designed for interoperable communication using HTTP and JSON-RPC, operates in a request-response model that can become a bottleneck in large-scale dynamic AI ecosystems. This synchronous, request-response approach mirrors the tightly coupled nature of monoliths, where components depend heavily on direct interactions. In A2A, each AI agent must wait for a response from another before proceeding, which causes difficulty to its ability to handle the high-volume, asynchronous interactions needed for real-time collaboration in large-scale AI ecosystems. As an example, in a smart city application, agents managing traffic, weather, and energy data might need to coordinate instantly. The sequential nature of HTTP calls creates delays, much like a monolithic system bogged down by a single change, making maintenance and scaling increasingly complex.&lt;/p&gt;

&lt;p&gt;In contrast, an event-driven architecture, such as one leveraging a message bus like Apache Kafka, could address these scalability issues by enabling asynchronous, decoupled communication. Kafka’s event-streaming capabilities allow systems to publish and subscribe to events in real time, distributing workloads efficiently across components, similar to how microservices break down monolithic systems into manageable, independent units. If A2A were to adopt an event-oriented approach, AI agents could publish tasks or data to a Kafka topic, allowing other agents to consume and process them at their own pace, reducing bottlenecks and improving scalability. This is where Sean Falconer's article shines: this shift would make A2A more adaptable to the demands of modern AI systems, just as businesses transitioning from monoliths to microservices would gain the agility to innovate and respond to market changes swiftly. I intentionally used the word "would," as software investments are not always successful, and I wanted to emphasize the inherent risks.&lt;/p&gt;

&lt;h3&gt;
  
  
  My Experience with Microservices
&lt;/h3&gt;

&lt;p&gt;I’ve long been a fan of microservices. In my work, I’ve experimented with ready-made systems, focusing on their interfaces rather than their internals, to adapt them for advanced business needs. For example, I once worked on integrating multiple digital asset processing services into a business workflow by building an interface that translated their inputs and outputs into the client’s format. Any problem with the integration would have to be dealt internally in the microservice implementation. This black-box approach simplified the process, letting me focus on delivering business value without getting bogged down in the system’s complexity.&lt;/p&gt;

&lt;p&gt;However, I learned that ignoring the underlying system entirely can backfire. In one project, performance issues arose because I didn’t account for how the microservice dependency on external integrations would affect the higher level functionalities. This taught me that while microservices and black-box thinking are powerful, understanding key internals is sometimes necessary - in this case, knowing the potential ways that it could fail, and its consequences were key to solve the problem.&lt;/p&gt;

&lt;p&gt;This experience connects to AI, particularly large language models (LLMs), which are often treated as black boxes. We use their inputs and outputs without fully understanding their inner workings. But as I found, we can’t always ignore the “sausages being made” if we want reliable functionality.&lt;/p&gt;

&lt;h2&gt;
  
  
  Event-Driven Architectures: Decoupling for Scalability
&lt;/h2&gt;

&lt;p&gt;Event-driven architectures (EDA) take decoupling to the next level. In EDA, services communicate by producing and consuming events—messages that signal something has happened, like a customer placing an order or a sensor detecting a change.&lt;/p&gt;

&lt;p&gt;Its setup is asynchronous, meaning producers don’t wait for consumers to respond, and consumers don’t need to know whom to send the event. This loose coupling enhances flexibility and scalability.&lt;/p&gt;

&lt;p&gt;Benefits:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Loose Coupling&lt;/strong&gt;: Services only need to know about events, not each other, making it easy to add or remove services without disrupting the system.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Scalability&lt;/strong&gt;: Events can be processed in parallel, handling high traffic efficiently. For example, during a Black Friday sale, an e-commerce system can process thousands of order events simultaneously.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Real-Time Responsiveness&lt;/strong&gt;: Systems react to events as they happen, enabling instant updates, like notifying a customer when their order ships.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Simplified Infrastructure&lt;/strong&gt;: As I’ve noted, EDA moves infrastructure logic (like coordinating services) out of the software layer, reducing complexity in the codebase.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Real-World Examples
&lt;/h3&gt;

&lt;p&gt;EDA is used across industries to solve real-world problems:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Heineken&lt;/strong&gt;: Connects over 4,500 internal applications using EDA, streamlining payments, logistics, and inventory management across legacy systems.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Federal Aviation Administration&lt;/strong&gt;: Uses EDA to share real-time air traffic data with airlines, ensuring timely updates without constant polling.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;E-commerce&lt;/strong&gt;: Companies like Amazon use EDA for real-time inventory updates, reducing stockouts and improving customer trust.
&amp;gt; Source: &lt;a href="https://solace.com/what-is-event-driven-architecture/" rel="noopener noreferrer"&gt;https://solace.com/what-is-event-driven-architecture/&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These examples show how EDA enables businesses to respond quickly to changes, a critical advantage in competitive markets.&lt;/p&gt;

&lt;h2&gt;
  
  
  Applying Event-Driven Architectures to AI Systems
&lt;/h2&gt;

&lt;p&gt;Event-driven architectures (EDA) offer significant benefits for AI systems, enabling real-time processing of vast datasets and seamless coordination among components. Google’s Agent2Agent (A2A) protocol exemplifies this potential by facilitating standardized communication between AI agents from different vendors, though it currently faces scalability challenges due to its reliance on HTTP and JSON-RPC rather than an event-streaming platform like Apache Kafka - as mentioned in Falconer's article.&lt;/p&gt;

&lt;p&gt;Its point-to-point communication model, where agents connect directly, struggles with enterprise-scale complexity:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Too Many Connections&lt;/strong&gt;: As agent numbers increase, direct connections create a complex web of dependencies.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Tight Coupling&lt;/strong&gt;: Agents depend on each other’s availability, limiting flexibility.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Limited Visibility&lt;/strong&gt;: Monitoring interactions across agents is difficult, hindering debugging and auditing.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is where I agree with Falconer: integrating an event-streaming platform like Apache Kafka could enhance A2A’s scalability and efficiency. Just to mention, here are a few reasons:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Decoupled Communication&lt;/strong&gt;: Agents publish events to Kafka topics, and others subscribe to relevant events, eliminating direct dependencies.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Scalability&lt;/strong&gt;: Kafka efficiently manages high event volumes, supporting large-scale AI ecosystems.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Real-Time Orchestration&lt;/strong&gt;: Events are processed instantly, enabling dynamic collaboration, such as multiple agents updating a customer profile simultaneously.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Observability&lt;/strong&gt;: Kafka’s event logs simplify monitoring and auditing of agent interactions.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example, in a customer service AI ecosystem, a chatbot agent could publish a customer query to a Kafka topic. A data analytics agent might subscribe to analyze sentiment, while a CRM agent updates the customer’s profile. This seamless interaction occurs without agents needing direct knowledge of each other, leveraging Kafka’s event-driven framework.&lt;/p&gt;

&lt;h3&gt;
  
  
  EDA and LLMs
&lt;/h3&gt;

&lt;p&gt;EDA’s principles also enhance large language models (LLMs) and AI services. LLMs, often complex and opaque, benefit from event-driven triggers, such as user feedback or new data, to update models incrementally without system-wide overhauls. For instance, a content creation AI could respond to an event signaling a new user-submitted topic, generating relevant material in real time. This approach mirrors microservices’ incremental evolution, ensuring AI systems remain responsive and adaptable as they integrate into business processes.&lt;/p&gt;




&lt;p&gt;I hope this inspires insightful considerations, just as Falconer’s article inspired me. Thanks Mike!&lt;/p&gt;

</description>
      <category>architecture</category>
      <category>programming</category>
      <category>eventdriven</category>
      <category>microservices</category>
    </item>
    <item>
      <title>Implementing Atomic Design Principles in Laravel</title>
      <dc:creator>Savio Resende</dc:creator>
      <pubDate>Wed, 05 Mar 2025 19:20:09 +0000</pubDate>
      <link>https://dev.to/savioresende/implementing-atomic-design-principles-in-laravel-nnn</link>
      <guid>https://dev.to/savioresende/implementing-atomic-design-principles-in-laravel-nnn</guid>
      <description>&lt;h3&gt;Introduction&lt;/h3&gt;

&lt;p&gt;Atomic design is a methodology developed by Brad Frost that allows developers to construct UIs from individual parts, starting from basic building blocks. These components or parts, named after chemistry's basic building blocks, include atoms, molecules, organisms, templates, and pages. In this blog post, we'll review how to use these principles in a Laravel application.&lt;/p&gt;

&lt;h3&gt;The Origins of Atomic Design&lt;/h3&gt;

&lt;p&gt;The Atomic Design methodology was conceived by &lt;a rel="noreferrer noopener" href="https://twitter.com/brad_frost"&gt;Brad Frost&lt;/a&gt;. The philosophy behind Atomic Design takes its inspiration from the world of chemistry. The idea is that all matter in the universe is made up of atoms, which combine to form molecules, which in turn combine to form more complex structures.&lt;/p&gt;

&lt;p&gt;In 2013, Frost first wrote about Atomic Design in a blog post, introducing a new way to think about constructing web interfaces. His methodology encourages designers and developers to break down interfaces into fundamental building blocks (which he named "atoms") and then hierarchically combine these blocks to form more complex, reusable components (or "molecules" and "organisms"). This concept was expanded further in his book, also titled "Atomic Design", which was published in 2016.&lt;/p&gt;

&lt;p&gt;Brad Frost's approach to design has resonated with many in web development for its logical, efficient, and maintainable way of constructing UIs. From a single atom to a complex interface, Atomic Design gives us a methodology for creating, testing and maintaining robust design systems. This makes it a natural fit for modern, component-based web frameworks like Laravel.&lt;/p&gt;

&lt;h3&gt;What is Atomic Design?&lt;/h3&gt;

&lt;p&gt;Atomic design, as said, involves creating interfaces deliberately and hierarchically. It breaks down UI into the following parts:&lt;/p&gt;


&lt;li&gt;
&lt;strong&gt;Atoms:&lt;/strong&gt; These are the most basic building blocks. Examples include buttons, input fields, and other HTML elements.&lt;/li&gt;


&lt;li&gt;
&lt;strong&gt;Molecules:&lt;/strong&gt; A group of atoms functioning together as a unit. Examples include a form element composed of label and input atoms.&lt;/li&gt;


&lt;li&gt;
&lt;strong&gt;Organisms:&lt;/strong&gt; Complex UI components composed of groups of molecules and/or atoms and molecules. Examples include a header consisting of logo, navigation, and search form molecules.&lt;/li&gt;


&lt;li&gt;
&lt;strong&gt;Templates:&lt;/strong&gt; Page-level objects that place components into a design and layout context. Templates lay out the design.&lt;/li&gt;


&lt;li&gt;
&lt;strong&gt;Pages:&lt;/strong&gt; These are specific instances of templates. Here, placeholder content is replaced with real content.&lt;/li&gt;

&lt;h3&gt;Example: Building a Blog Comment System&lt;/h3&gt;

&lt;p&gt;Let's go through an example where we create a simple commenting system for a blog post. We'll start from atoms and build our way up to a complete page.&lt;/p&gt;

&lt;h4&gt;Atoms&lt;/h4&gt;

&lt;p&gt;The basic building blocks of your interface, like an input field:&lt;/p&gt;

&lt;pre data-lang="PHP"&gt;&lt;code&gt;&amp;lt;!-- resources/views/components/atoms/input.blade.php --&amp;gt;

&amp;lt;input {{ $attributes-&amp;gt;merge(['class' =&amp;gt; 'form-input']) }} /&amp;gt;&lt;/code&gt;&lt;/pre&gt;

&lt;h4&gt;Molecules&lt;/h4&gt;

&lt;p&gt;Molecules are groups of atoms functioning together. For example, a comment form molecule that uses the input atom:&lt;/p&gt;

&lt;pre data-lang="PHP"&gt;&lt;code&gt;&amp;lt;!-- resources/views/components/molecules/comment-form.blade.php --&amp;gt;

&amp;lt;form method="POST" action="/comment"&amp;gt;
    @csrf
    &amp;lt;x-atoms.input type="text" name="username" placeholder="Enter your username" /&amp;gt;
    &amp;lt;x-atoms.input type="text" name="comment" placeholder="Enter your comment" /&amp;gt;
    &amp;lt;button type="submit"&amp;gt;Submit&amp;lt;/button&amp;gt;
&amp;lt;/form&amp;gt;&lt;/code&gt;&lt;/pre&gt;

&lt;h4&gt;Organisms&lt;/h4&gt;

&lt;p&gt;Organisms are groups of molecules joined together. In our example, a comments section that includes multiple instances of the comment form:&lt;/p&gt;

&lt;pre data-lang="PHP"&gt;&lt;code&gt;&amp;lt;!-- resources/views/components/organisms/comments-section.blade.php --&amp;gt;

&amp;lt;section&amp;gt;
    &amp;lt;h2&amp;gt;Comments&amp;lt;/h2&amp;gt;
    &amp;lt;x-molecules.comment-form /&amp;gt;
    @foreach($comments as $comment)
        &amp;lt;article&amp;gt;
            &amp;lt;h3&amp;gt;{{ $comment-&amp;gt;username }}&amp;lt;/h3&amp;gt;
            &amp;lt;p&amp;gt;{{ $comment-&amp;gt;body }}&amp;lt;/p&amp;gt;
        &amp;lt;/article&amp;gt;
    @endforeach
&amp;lt;/section&amp;gt;&lt;/code&gt;&lt;/pre&gt;

&lt;h4&gt;Templates&lt;/h4&gt;

&lt;p&gt;Templates place components into a layout. For our blog, we can create a template for a blog post that includes the comment section:&lt;/p&gt;

&lt;pre data-lang="PHP"&gt;&lt;code&gt;&amp;lt;!-- resources/views/components/templates/blog-post.blade.php --&amp;gt;

&amp;lt;article&amp;gt;
    &amp;lt;h1&amp;gt;{{ $post-&amp;gt;title }}&amp;lt;/h1&amp;gt;
    &amp;lt;p&amp;gt;{{ $post-&amp;gt;body }}&amp;lt;/p&amp;gt;
&amp;lt;/article&amp;gt;

&amp;lt;x-organisms.comments-section :comments="$post-&amp;gt;comments" /&amp;gt;&lt;/code&gt;&lt;/pre&gt;

&lt;h4&gt;Pages&lt;/h4&gt;

&lt;p&gt;Finally, pages are instances of templates that show what a UI looks like with real representative content in place. A blog page that makes use of the blog post template would look like this:&lt;/p&gt;

&lt;pre data-lang="PHP"&gt;&lt;code&gt;&amp;lt;!-- resources/views/pages/blog.blade.php --&amp;gt;

@extends('layouts.app')

@section('content')
    @foreach($posts as $post)
        &amp;lt;x-templates.blog-post :post="$post" /&amp;gt;
    @endforeach
@endsection&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;Conclusion&lt;/h3&gt;

&lt;p&gt;You can create reusable, maintainable, and organized code by structuring your Laravel components according to the atomic design methodology. This approach allows for a more efficient and scalable development process. When building your applications, always consider how you can break down your interfaces into these atomic components to make your work easier and your code cleaner.&lt;/p&gt;

&lt;p&gt;Remember, building reusable components with a clear, well-defined purpose and interface is key to creating reusable components. It's also important to ensure they are well-documented and that they are easy to test and maintain. Happy coding!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>laravel</category>
      <category>softwaredesign</category>
    </item>
    <item>
      <title>Avoid This Laravel Enum Trap: Learn How to Fix It Before It Breaks Your Project!</title>
      <dc:creator>Savio Resende</dc:creator>
      <pubDate>Sun, 02 Feb 2025 20:12:07 +0000</pubDate>
      <link>https://dev.to/savioresende/avoid-this-laravel-enum-trap-learn-how-to-fix-it-before-it-breaks-your-project-b97</link>
      <guid>https://dev.to/savioresende/avoid-this-laravel-enum-trap-learn-how-to-fix-it-before-it-breaks-your-project-b97</guid>
      <description>&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.amazonaws.com%2Fuploads%2Farticles%2F1tx5e9xx0jnanoiccx3j.png" 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.amazonaws.com%2Fuploads%2Farticles%2F1tx5e9xx0jnanoiccx3j.png" alt="Image description" width="800" height="800"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Enums are a way to represent a fixed set of values in your application, often tied to specific use cases like statuses, roles, or categories. Laravel allows developers to use type casting with enums to ensure these values are strictly typed when retrieved from the database, making the code cleaner and reducing potential bugs. However, while type casting might initially seem like an organized and efficient solution, it can lead to significant challenges as your software evolves and your codebase grows, especially in legacy systems.&lt;/p&gt;

&lt;p&gt;In an ideal software development world, every new task starts fresh. You begin by creating a clean database and then you tailor it to the task at hand, ensuring that your initial dataset is perfectly seeded to reproduce specific scenarios. This allows you to write precise tests to validate those scenarios while covering additional cases as they happen. When new or unique scenarios are required, the process becomes iterative—you intentionally design and implement them with a clear focus.&lt;/p&gt;

&lt;p&gt;This approach assumes you’re starting with greenfield projects or maintaining codebases with flawless processes. But in the real world, especially when maintaining legacy code, things aren’t so simple. As software evolves, enums and their use cases change. New cases are added, old cases become deprecated, and business requirements shift over time. Deprecating enum cases introduces complexity. When you deprecate a specific enum case, you might mark it as such in your code, intending to remove it in the future. But if that enum value is persisted in your database, removing it becomes problematic.&lt;/p&gt;

&lt;p&gt;Here’s where things get tricky: (1) If you remove the deprecated case from your enum class but it still exists in your database, Laravel's magic type casting in Eloquent models will throw the error "X is not a valid backing value for enum EnumClassY." (2) Debugging becomes a nightmare. Since the error originates from Laravel’s magical type casting, it’s often difficult to trace its source, much like debugging errors in virtualized DOM frameworks in JavaScript. Without prior experience with this specific issue, you might struggle to find a solution.&lt;/p&gt;

&lt;p&gt;This happens for 3 reasons:&lt;/p&gt;

&lt;p&gt;First, Laravel’s Eloquent models simplify many tasks, but their magic can become a double-edged sword. When enum casting fails, you’re left without a clear stack trace or error source.&lt;br&gt;
Second, even if you’ve deprecated a value in your code, that value may still exist in your database. Deleting data to resolve the issue is often not an option, especially when working with production environments where data integrity is critical. Suddenly, you might see an error that you can’t trace back to its source, and you’re left with a broken system.&lt;br&gt;
Third, imagine deploying a version of your application where a new enum case has been added via a migration. If a bug forces you to roll back to an older version of your codebase that doesn’t yet recognize the new case, you’re in trouble. The old code won’t know how to handle the new enum, causing further errors.&lt;br&gt;
After battling with these issues, here are 2 insights:&lt;/p&gt;

&lt;p&gt;Avoid Over-reliance on Enum Casting: while it’s tempting to cast enums directly in your models, remember that doing so ties your database data too tightly to your code.&lt;br&gt;
Making casting intentional by using a custom enum caster is ideal since it allows you to handle errors gracefully. Instead of letting uncaught exceptions occur when trying to use an enum that is deprecated or nonexistent, you can implement logic to report the issue clearly. This approach ensures the system fails in a controlled and debuggable manner, reducing confusion and enabling quicker resolution of problems.&lt;br&gt;
Let’s consider a concrete example to understand how to handle enums in a way that avoids issues. Imagine you’re adding a type column to your users table. This column specifies the type of user, and at a certain point, your requirements define three types: bot, robot, and user. Over time, the requirements evolve: robot type is deprecated. Despite deprecation, some records in your database still reference the robot type, and some business logic relies on its existence. This includes contextual data, like model states, which were influenced by the robot type. Workflows and processes were built around the robot type, and removing it entirely could break your application.&lt;/p&gt;

&lt;p&gt;Now, you face a decision: should you remove the robot value from the type column, or handle it differently? Simply deleting the value introduces risks, such as breaking existing business rules or losing historical context. However, leaving it indefinitely can lead to technical debt and confusion. The best approach is a middle ground, leveraging Laravel’s custom casters to handle these scenarios gracefully.&lt;/p&gt;

&lt;p&gt;A custom caster allows you to customize how Eloquent models handle type casting, avoiding uncaught exceptions while giving you the flexibility to decide how to handle deprecated or invalid enum values (instead of having to handle with an exception every time you instantiate your users).&lt;/p&gt;

&lt;p&gt;In the Listing 1 is my proposal Caster:&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="cp"&gt;&amp;lt;?php&lt;/span&gt;  

&lt;span class="kn"&gt;namespace&lt;/span&gt; &lt;span class="nn"&gt;App\Casts&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\Enums\UserType&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\Contracts\Database\Eloquent\CastsAttributes&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\Database\Eloquent\Model&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;UserTypeCast&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;CastsAttributes&lt;/span&gt;  
&lt;span class="p"&gt;{&lt;/span&gt;  
    &lt;span class="cd"&gt;/**  
     * @param  array&amp;lt;string, mixed&amp;gt;  $attributes  
     */&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;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Model&lt;/span&gt; &lt;span class="nv"&gt;$model&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nv"&gt;$key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;mixed&lt;/span&gt; &lt;span class="nv"&gt;$value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;array&lt;/span&gt; &lt;span class="nv"&gt;$attributes&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kt"&gt;mixed&lt;/span&gt;  
    &lt;span class="p"&gt;{&lt;/span&gt;  
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;rescue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;UserType&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;tryFrom&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$value&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;report&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  
    &lt;span class="p"&gt;}&lt;/span&gt;  

    &lt;span class="cd"&gt;/**  
     * @param  array&amp;lt;string, mixed&amp;gt;  $attributes  
     */&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;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Model&lt;/span&gt; &lt;span class="nv"&gt;$model&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nv"&gt;$key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;mixed&lt;/span&gt; &lt;span class="nv"&gt;$value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;array&lt;/span&gt; &lt;span class="nv"&gt;$attributes&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kt"&gt;mixed&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;$value&lt;/span&gt; &lt;span class="k"&gt;instanceof&lt;/span&gt; &lt;span class="nc"&gt;UserType&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="nv"&gt;$value&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nv"&gt;$value&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;Listing 1: Custom Caster for UserType&lt;/p&gt;

&lt;p&gt;Takeaways from this strategy:&lt;/p&gt;

&lt;p&gt;Graceful Failure: Instead of throwing an uncaught exception, the custom caster logs and reports invalid enum values, enabling you to track issues.&lt;/p&gt;

&lt;p&gt;Flexibility: Returning null allows you to decide at runtime how to handle deprecated or missing values without breaking the application.&lt;/p&gt;

&lt;p&gt;Maintainability: This approach prevents the accumulation of technical debt by encouraging proactive handling of edge cases and legacy data.&lt;/p&gt;

&lt;p&gt;User-side Decisions: By not breaking the application, you enable system users to decide how to handle deprecated values, such as converting to a default type or retiring the record.&lt;/p&gt;

&lt;p&gt;Type casting for enums in Laravel can seem like an elegant solution at first glance. But as your application grows, what starts as a helpful feature can quickly turn into a maintenance burden. By understanding the pitfalls and planning for the future, you can avoid getting trapped by past decisions. Remember, experience often teaches us that simplicity and foresight are the keys to robust, maintainable software.&lt;/p&gt;

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