Search code examples
laravellaravel-5laravel-bladelaravel-form

How to use style in Laravel Blade form in select option


I want to have a form like this:

<form>
  <select name="color">
    <option style="background: red">RED</option>
    <option style="background: green">GREEN</option>
    <option style="background: blue">BLUE</option>
  </select>
</form>

How can I do this with Laravel Blade?


Solution

  • I guess you're asking about how to build select list with Laravel Collective Form:: and add custom class to each option.

    In this case, I would recommend you to create custom macro.

    It's easy to define your own custom Form class helpers called "macros". Here's how it works. First, simply register the macro with a given name and a Closure

    Form::macro('customCssSelect', function()
    {
        return '<select>your HTML and PHP here</select>';
    });
    

    And then use it to build dropdown list:

    Form::customCssSelect();