Search code examples
phplaravelformsfacadepluck

Laravel form face use $key => $value ($value2) instead of $key => $value


Sorry if the title is misleading...

I have the following to create a select box using the laravel Form facade:

{{ Form::select('category_select', $categories_select, null, array('class' => 'selectpicker show-tick', 'data-live-search' => 'true', 'id' => 'category_select')) }}

Now $catgories_select is a pluck() of id and name.

I want to do the same for another select (Tax rules) but there I want to have it like the following:

<option value="id">$value1 ($value2)</option>

How can I do this?


Solution

  • If you pluck the models before the view, you'll need two collections. But you could just do one query, then pluck inline. Let's assume $categories is your queried collection before the pluck:

    {{ Form::select('category_select', $categories->pluck('name', 'id'), null) }}
    

    Then on the other you'd pluck a mutated property:

    {{ Form::select('category_select_tax', $categories->pluck('compositeName', 'id'), null) }}
    

    And then, on your Category model you'd have:

    public function getCompositeNameAttribute()
    {
        return "{$this->name} ({$this->value2})";
    }