Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ramonph's avatar

passing value to a blade input component

hi, i created a component name form-input. the code is very simple.
<input {{ $attributes->merge(['class'=>"block flex-1 border-0 bg-transparent py-1.5 pl-1 text-gray-900 placeholder:text-gray-400 focus:ring-0 sm:text-sm/6"]) }} />

this is how i use the component and pass the attributes type="text" name="item" id="item" value = "12345" , it doesnt get parsed.

     <x-form-input type="text" name="item" id="item" value = "12345" />  -- error doesnt parse 
      <x-form-input type="text"  name="item" id="item"/>  -- without value attribute it parses. 

Can someone show me what i missed why it doesnt show when i pass value to the component? Inspecting the browser it still shows the x-form-input so i concluded the component doesnt parse. thank you, Mon

0 likes
1 reply
LaryAI's avatar
Level 58

The issue you're encountering is likely due to the way Blade components handle attributes. When you pass attributes to a Blade component, they should be properly merged and rendered in the final HTML. However, if the value attribute is not being parsed correctly, it might be due to how the attributes are being merged or how the component is being used.

Here's a step-by-step solution to ensure that the value attribute is correctly parsed and rendered:

  1. Ensure Proper Attribute Merging: Make sure that the $attributes are being merged correctly in your component. The merge method should be used to combine any additional attributes passed to the component.

  2. Check for Typos or Syntax Errors: Double-check your component and the way you're using it to ensure there are no typos or syntax errors.

  3. Use the @props Directive: If you're using Laravel 7 or later, you can use the @props directive to define default values for your component's attributes. This can help ensure that all attributes are correctly recognized.

Here's an example of how you can define and use your form-input component:

Component Definition (form-input.blade.php)

@props(['value' => ''])

<input {{ $attributes->merge(['class' => 'block flex-1 border-0 bg-transparent py-1.5 pl-1 text-gray-900 placeholder:text-gray-400 focus:ring-0 sm:text-sm/6']) }} value="{{ $value }}" />

Using the Component

<x-form-input type="text" name="item" id="item" value="12345" />

Explanation

  • @props Directive: This directive is used to define default values for the component's attributes. In this case, it sets a default empty string for value if it's not provided.

  • value="{{ $value }}": This ensures that the value attribute is explicitly set in the input element, using the value passed to the component.

By following these steps, the value attribute should be correctly parsed and rendered in your HTML output. If you continue to experience issues, ensure that your Blade component file is correctly named and located in the appropriate directory (usually resources/views/components).

Please or to participate in this conversation.