Mahamudul Hasan Rubel
HomeAboutProjectsSkillsExperienceBlogPhotosContact
Mahamudul Hasan Rubel

Senior Software Engineer crafting high-performance web applications and SaaS platforms.

Navigation

  • Home
  • About
  • Projects
  • Skills
  • Experience
  • Blog
  • Photos
  • Contact

Get in Touch

Available for senior/lead roles and consulting.

bd.mhrubel@gmail.comHire Me

© 2026 Mahamudul Hasan Rubel. All rights reserved.

Built with using Next.js 16 & Tailwind v4

Back to Blog
LaravelPHPJune 22, 20264 min read

Laravel Blade components: Building reusable UI elements from scratch

Laravel Blade components help you build reusable, maintainable UI elements. Learn how to structure your code, manage attributes, and simplify your templates.

LaravelPHPBladeUIWeb DevelopmentFrontendComponentsTutorial

Last month, I spent three days refactoring a dashboard that had become a graveyard of copy-pasted HTML. Every button, every card, and every input field existed in four different versions across the codebase, and changing a single CSS class meant hunting through a dozen Blade files. If you're feeling that same pain, it's time to stop treating your views like static documents and start using Laravel Blade components to build a proper UI system.

Components aren't just about reducing line count; they’re about creating a single source of truth for your UI. When you build them correctly, you stop worrying about whether your "Delete" button looks the same on the index page as it does in the modal.

Getting started with Blade Components

To create your first component, you’ll use the Artisan CLI. Run this command in your terminal:

Bash
php artisan make:component Alert

This generates two files: a class at app/View/Components/Alert.php and a view template at resources/views/components/alert.blade.php.

At first, I used to just create anonymous components—those without a backing class—for simple things. That works fine for a static icon, but as soon as you need logic (like determining if an alert should be dismissible based on user permissions), you need the class. The logic lives in the PHP file, keeping your template clean.

Managing component attributes

One of the biggest hurdles for juniors is handling HTML attributes like class or id. You don't want to manually define every possible HTML attribute in your constructor. Instead, Laravel provides the $attributes bag, which is a lifesaver.

In your alert.blade.php, you can merge custom classes with defaults like this:

HTML
style="color:#808080"><style="color:#4EC9B0">div {{ $attributes->merge(['class' => 'alert alert-info p-4 rounded']) }}>
    {{ $slot }}
style="color:#808080"></style="color:#4EC9B0">div>

Now, when you use the component in a view, you can pass extra classes without breaking the base styling:

HTML
style="color:#808080"><style="color:#4EC9B0">x-alert class="mt-4 shadow-lg">
    Something went wrong!
style="color:#808080"></style="color:#4EC9B0">x-alert>

This approach saved me around 200 lines of code during a recent refactor. It’s significantly cleaner than the old way of manually passing variables for every single HTML property. Just like when you Mastering Laravel View Composers: Injecting Shared Data Across Your Templates, leveraging these structural tools keeps your templates focused on display rather than data manipulation.

When components get complex

I once tried to build a "DataGrid" component that handled everything—sorting, filtering, and pagination. It became a 400-line monster that was impossible to debug. Don't do that.

If your component is getting too heavy, it’s a signal to break it down. Think of it like a function: one component should do one thing well. If you find yourself needing to inject complex data from multiple sources, you might also look into Laravel Blade custom directives: A guide to cleaner templates to handle specific formatting logic that doesn't belong in a component.

Why Blade components win

The beauty of Blade components lies in how they force you to think about interfaces. When you define a component, you're essentially defining an API for your UI.

  1. Encapsulation: Styles and structure live together.
  2. Reusability: Write it once, use it everywhere.
  3. Type-safety: You can type-hint properties in your constructor.

I’ve seen developers move to Laravel Livewire 3: The Future of Frontend because they wanted reactivity, but the truth is, most of the architecture remains identical. If you master Blade components now, transitioning to Livewire later feels like an incremental upgrade rather than a total rewrite.

Common pitfalls

Don't over-engineer. I’ve seen projects where every single <div> was turned into a component. That’s overkill. If you aren't reusing it in at least three places, just write the HTML.

Also, keep your component logic simple. If your Alert.php class is querying the database, you’ve gone too far. Components should be "dumb" renderers. Fetch the data in your controller or via a view composer, then pass it to the component.

Frequently Asked Questions

Q: Should I use Anonymous Components or Class-based Components? A: Use anonymous components for simple, display-only elements (like a wrapper div). Use class-based components when you need to process data, set default values, or perform validation before rendering.

Q: Can I nest components? A: Absolutely. You can call <x-button /> inside an <x-form /> without any issues. It’s how you build complex UI systems.

Q: Is it faster than standard includes? A: Performance-wise, the difference is negligible. The real gain is developer speed and code maintainability.

Moving forward

Building a library of reusable components is an investment. You’ll spend more time upfront creating the component, but you'll make that time back tenfold when you need to update your design system. I’m still refining my own component library, and I’m currently experimenting with how to better handle complex form validation errors within these components. It’s a work in progress, but that’s the nature of PHP web development. Don't be afraid to delete a component and start over if it isn't serving you; that's just part of the process.

Back to Blog

Similar Posts

Close-up of three wooden arrows pointing in opposite directions on a beige surface.
LaravelPHPJune 21, 20264 min read

Laravel Blade custom directives: A guide to cleaner templates

Laravel Blade custom directives help you remove repetitive logic from your views. Learn how to build and register them to keep your code DRY and readable.

Read more
Laravel
PHP
June 22, 2026
4 min read

Laravel Notifications: A Beginner’s Guide to Multi-Channel Alerts

Laravel Notifications help you send alerts via email, SMS, and database. Learn how to implement multi-channel messaging in your app with this practical guide.

Read more
LaravelPHPJune 22, 20264 min read

Laravel Error Handling: A Practical Guide for Cleaner Code

Master Laravel error handling by building custom exceptions and global handlers. Stop cluttering your controllers and start managing failures gracefully.

Read more