Laravel set option sélectionnée si l'option id est dans la liste du réseau en vue de la lame

J'ai fait une requête dans mon contrôleur et de l'envoyer à ma lame :

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]);
    }

et quand je l'appelle {{ $developer_contractor_association->defect_types }} dans ma lame, j'obtiens ceci :

[
    {
        "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}
    }
]

Maintenant, j'ai un champ de sélection de la liste de tous les type de défaut qui existait :

<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(App\DefectType::select('id','title')->get() as $defect_type)
        <option value="{{$defect_type->id}}">{{$defect_type->title}}</option>
    @endforeach
</select>

Quand la population de l'option pour le champ select, Comment puis-je vérifier si le id dans defect_type_id JSON et activez l'option sélectionnée. Quelque chose comme ceci :

<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(App\DefectType::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>
+1
2019-09-18 09:43:09
source
0 réponses

Voir d'autres questions sur les étiquettes