Skip to content
Advertisement

Laravel set option to selected if option id is in an array list in blade view

I made query in my controller and send it to my blade :

public function editContractorAssociation(DeveloperContractorAssociation $developer_contractor_association, Request $request)
    {
        $id = $request->id;
        $developer_contractor_association = DeveloperContractorAssociation::whereHas('defect_types', function ($query) use($id) {
            $query->where('developer_contractor_associations.id', $id);
        })->orwhereHas('contractor', function ($query) use($id) {
            $query->where('developer_contractor_associations.id', $id);
        })->first();
        return view('dev-admin.contractors.associations.edit', ['developer_contractor_association' => $developer_contractor_association]);
    }

and when I call {{ $developer_contractor_association->defect_types }} in my blade, I get this :

[
    {
        "id":2,
        "title":"Wiring",
        "details":"Fix wiring",
        "created_by":"22",
        "created_at":"2019-09-04 11:39:48",
        "updated_at":"2019-09-04 11:39:48",
        "deleted_at":null,
        "is_custom":1,
        "developer_id":1,
        "pivot":{"dca_id":87,"defect_type_id":2}},
        {"id":3,"title":"Plumbing",
            "details": "Fix Pipe",
            "created_by":"22",
            "created_at":"2019-09-04 11:40:07",
            "updated_at":"2019-09-04 11:40:07",
            "deleted_at":null,
            "is_custom":1,
            "developer_id":1,
        "pivot":{"dca_id":87,"defect_type_id":3}
    }
]

Now I have a select field that list all the defect type that existed :

<select class="selectpicker {{ $errors->has('defect-type-id') ? 'is-invalid' : '' }}" name="defect-type-id[]" id="defect-type-id" multiple data-style="selectpicker-style" data-width="100%" title="Defect Types">
    @foreach(AppDefectType::select('id','title')->get() as $defect_type)
        <option value="{{$defect_type->id}}">{{$defect_type->title}}</option>
    @endforeach
</select>

When population the option for the select field, How can I check if the id is in the defect_type_id JSON and set the option to selected. Something like this :

<select class="selectpicker {{ $errors->has('defect-type-id') ? 'is-invalid' : '' }}" name="defect-type-id[]" id="defect-type-id" multiple data-style="selectpicker-style" data-width="100%" title="Defect Types">
    @foreach(AppDefectType::select('id','title')->get() as $defect_type)
        {{if $defect_type->id in $defect_type_list}}
            <option value="{{$defect_type->id}}">{{$defect_type->title}} selected</option>
        {{else}}
        <option value="{{$defect_type->id}}">{{$defect_type->title}}</option>
    @endforeach
</select>

Advertisement

Answer

Assuming your defect_types relationship returns a Laravel Collection, you can use a couple of the Collection methods:

@foreach(AppDefectType::select('id','title')->get() as $defect_type)    
    <option value="{{$defect_type->id}}"
        @if($developer_contractor_association->defect_types->pluck('id')->contains($defect_type->id))
            selected
        @endif
    >
        {{$defect_type->title}}
    </option>
@endforeach

pluck('id') returns a Collection of defect_type id and you can then check if the current iteration of all defect_type is in that Collection with contains($defect_type->id).

You may use containsstrict() instead of contains() as that makes a strict comparison.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement