phplaravellaravel-bladehtml-select

Assign value to select in Laravel Collectives


I am not sure if I do anything right here, and I cannot really find any documentation about this. Good documentation advice would also be nice. I try to fill a select from Laravel Collectives but I am not sure how to set the id field correctly because right now it is just returning the position. So I pass a table from my controller:

$stuff_types = DB::table('companies')->where('active', '1')->pluck('name','id');

and call this in my view:

{{Form::select('stuff_types', array_merge(array('default' => 'Please Select'), $stuff_types->all('name', 'id')), null, ['class'=>'form-control', 'id'=>"order-type"]) }}

I would now like to assign what is the displayed name and what is the value behind so that I can call it in javascript. If I do it without Collectives, it looks like this:

<select name="categories" id="categories" class="form-control">    
                                        <option value="-5">Pleace Select</option>                                    
                                    @foreach($positions as $sector)
                                        @if ($sector->id == 2 || $sector->id == 3)
                                            <option value="{{ $sector->id }}">{{ $sector->name }}</option>
                                        @endif
                                    @endforeach
                                </select>

Also can someone give me a hint how to verify an array instead of checking each value by one:

$sector->id == 2 || $sector->id == 3

Thanks in advance

Stephan


Update 2019-08-14

After the help of nakov, we could figure out the problem and solve it like this:

Option1:

Controller

$stuff_types = DB::table('companies')->where('active', '1')->pluck('name','id');
$stuff_types->prepend('Please Select', '');

View:

{{Form::select('stuff_types', $stuff_types, null, ['class'=>'form-control', 'id'=>"order-type"]) }}

Option 2:

Controller

$stuff_types = DB::table('companies')->where('active', '1')->pluck('name','id')->toArray();

View:

{{Form::select('stuff_types', ['' => 'Please Select'] + $stuff_types, null, ['class'=>'form-control', 'id'=>"order-type"]) }}

Solution

  • The second parameter in the element should be the array of the elements, so you already are passing it the right way from your controller, it should be an array like this:

    [1 => 'Title 1', 2 => 'Title 2' ....]
    

    So what you need is this instead:

    {{Form::select('stuff_types', array_merge(array('default' => 'Please Select'), $stuff_types), null, ['class'=>'form-control', 'id'=>"order-type"]) }}
    

    just delete the call to all :)

    The third parameter which is null at the moment, is the value that you need pre-selected.

    -- EDIT

    In your controller add this instead:

    $stuff_types = DB::table('companies')->where('active', '1')->pluck('name','id')->toArray();
    

    And use the above code for the select that I've shared.