Passing conditional disabled attribute to blade component with ternary operator
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Conditionally Disabling Components: Mastering Attributes in Laravel Blade
As developers working with modern frameworks like Laravel, we often need to control the appearance and behavior of UI elements based on complex application logic. A common requirement is conditionally applying attributes, such as disabled, based on data retrieved from the backend. This post dives into a specific challenge: how to use conditional logic, specifically the ternary operator, to pass dynamic attributes to Blade components.
The Challenge: Mixing Logic and HTML Attributes
You are attempting to embed complex PHP logic directly within the attribute list of a Blade component tag to conditionally add a disabled attribute:
<x-button {{!$aircraftType->canIslandBuild($island) ? 'disabled' : ''}}>
Build
</x-button>
While this approach seems logically sound—using the ternary operator to output either the string 'disabled' or an empty string—it often results in syntax errors like the one you encountered (syntax error, unexpected token "endif").
Why the Error Occurs
The issue stems from how Blade parses attributes versus raw PHP execution within HTML tags. When Blade processes the opening tag of a component, it expects standard attribute syntax (e.g., class="...", id="..."). Introducing complex control structures like ! and endif directly into this context confuses the parser because it is not expecting procedural PHP block syntax inside an attribute definition. Blade needs the logic to be evaluated before rendering the HTML structure, typically in the surrounding view scope.
Best Practices for Conditional Attributes on Components
The most robust and maintainable way to handle conditional attributes on components is to separate the logic (determining the state) from the presentation (rendering the HTML). Components should focus solely on how they look, not on complex data fetching logic.
Method 1: Conditional Rendering in the Parent View (Recommended)
The cleanest approach is to perform the conditional check in the parent Blade view where the component is being called. This ensures that the flow of control remains within the standard Blade environment, making debugging simpler and adhering to good separation of concerns—a principle central to building scalable applications, much like the principles found when architecting solutions on platforms like Laravel Company.
Here is how you would implement this correctly:
@if($aircraftType->canIslandBuild($island))
{{-- If true, pass the 'disabled' attribute --}}
<x-button disabled>
Build
</x-button>
@else
{{-- If false, render the button normally --}}
<x-button>
Build
</x-button>
@endif
This pattern is explicit, readable, and avoids the syntax errors associated with embedding raw PHP logic directly into component attributes.
Method 2: Passing Attributes as an Array (For Complex Scenarios)
If you absolutely must pass dynamic attributes, especially when dealing with many conditions or complex data structures, it is often better to build an array of attributes in your controller or view and pass that array to the component. The component then safely merges these attributes.
Parent View:
@php
$buttonAttributes = [];
if ($aircraftType->canIslandBuild($island)) {
$buttonAttributes['disabled'] = true;
}
@endphp
<x-button :attributes="$buttonAttributes">
Build
</x-button>
Component (x-button.blade.php):
Your component can then safely iterate over the attributes and apply them to the underlying HTML element, ensuring that all attribute values are properly escaped and formatted:
<button {{ $attributes->merge(['class' => 'inline-flex']) }}>
{{ $slot }}
</button>
This method cleanly separates data preparation from rendering, which is a core tenet of efficient code design.
Conclusion
While the temptation to use the ternary operator directly within a component tag is understandable for brevity, it often leads to parsing errors because Blade expects structured attribute definitions rather than procedural PHP flow control. For conditional attributes like disabled, the best practice is to delegate the decision-making logic to the parent view using standard Blade directives (@if). This results in code that is more robust, easier to debug, and adheres to the principles of clean, maintainable architecture seen throughout modern Laravel development.