我正在尝试在刀片文件中显示多个选定的值,但是应该选择的值都不是,我不确定为什么。
控制器
public function getEdit($productId)
{
$country_id = $product->region_country_id;
$regions = Region::where("country_id",$country_id)
->where("activation",1)->get();
$countries = Country::getActiveCountry();
return View::make('product.edit',
[
'regions' => $regions,
'countries' => $countries,
]
);
}
视图
<div class="col-lg-3">
<select class="form-control" id="region_country_id" name="region_country_id[]" multiple style="height: 10%">
{{$selected = explode(",",$product->region_country_id);}}
<?php foreach ($countries as $key => $value) { ?>
<option value="<?php echo $value->id; ?>" <?php in_array($value->id,$selected) ? 'selected' : '' ?>><?php echo $value->name;?></option>
<?php }
?>
</select>
<select class="form-control" id="region_id" name="region_id[]" style="margin-top: 10px;height: 10%" multiple>
{{$selected = explode(",",$product->region_id);}}
<?php if(Session::get('branch_access') != 1){?>
<option value="">All region</option>
<?php } ?>
<?php foreach ($regions as $key => $value) { ?>
<option value="<?php echo $value->id; ?>" <?php in_array($value->id,$selected) ? 'selected' : '' ?>><?php echo $value->region; ?></option>
<?php } ?>
</select>
</div>
答案 0 :(得分:1)
好像您在if语句中缺少额外的=。 !==
,而不是!=
。
此外,没有用于在刀片模板内部声明变量的语法,因此不建议这样做。 {{ $stuff }}
是回显的语法。
在您的控制器中,您应该为$selected
声明变量,并使用它们构建视图。
这是使用刀片语法的模板的更合适版本。
<select class="form-control" id="region_country_id" name="region_country_id[]" multiple style="height: 10%">
@foreach($countries as $country)
<option value="{{$country->id}}" {{ in_array($country->id, $cselected) ? 'selected' : '' }}>
{{$country->name}}
</option>
@endforeach
</select>
<select class="form-control" id="region_id" name="region_id[]" style="margin-top: 10px;height: 10%" multiple>
<option value="">All region</option>
@if(session()->get('branch_access') !== 1))
@foreach($regions as $region)
<option value="{{$region->id}}" {{ in_array($region->id, $rselected) ? 'selected' : '' }}>
{{$region->region}}
</option>
@endforeach
@endif
</select>
还有您的控制器:
<?php
public function getEdit($productId) {
$country_id = $product->region_country_id;
$regions = Region::where([
['country_id', ,'=', $country_id],
['activation', '=', 1]
])->get();
$countries = Country::getActiveCountry();
return view('product.edit', [
'regions' => $regions,
'countries' => $countries,
'cselected' => explode(',', $product->region_country_id),
'rselected' => explode(',', $product->region_id),
]);
}