我在laravel 8中使用了多对多关系。我有两个模型:房间和服务。我已经将一些服务附加到特定的聊天室,现在,当我想编辑这个特定的聊天室时,我需要它来显示哪些服务被选中,哪些没有被选中。
这是我正在使用的代码:
<select class="form-select" name="service_id[]" id="service_id" multiple>
@foreach($services as $service)
<option value="{{ $service->id }}" @if($rooms->services->in_array()) selected @endif>{{ $service->name }}</option>
@endforeach
</select>有什么建议吗?
发布于 2021-04-04 13:24:30
您可以执行以下操作:
@if(in_array($service->id, $rooms->services->pluck('id')->toArray(), true)) selected @endif发布于 2021-04-04 08:06:26
在你的控制器上试试这个:
$RoomServicesIds = [];
foreach($room->services as $service ){
array_push($RoomServicesIds ,$service->id);
}然后在刀片文件中返回$RoomServicesIds变量
在您的刀片文件中,将条件更改为该条件:
@if(in_array($service->id,$RoomServicesIds)) selected @endifhttps://stackoverflow.com/questions/66935045
复制相似问题