简体   繁体   English

无法在编辑刀片中显示多个选定的值

[英]unable to display multiple selected values in edit blade

I am trying to display multiple selected values in my blade file however none of the values that should be selected are and I'm not sure why.我试图在我的刀片文件中显示多个选定的值,但是没有一个应该被选择的值,我不知道为什么。

controller控制器

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

view看法

 <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>

Looks like you're missing an extra = in your if statement.看起来您在 if 语句中缺少一个额外的 = 。 !== instead of != . !==而不是!=

Also, there is no syntax for declaring a variable inside of a blade template and it is discouraged to do so.此外,没有用于在刀片模板内声明变量的语法,因此不鼓励这样做。 {{ $stuff }} is syntax for echoing. {{ $stuff }}是回显的语法。

In your controller you should declare variables for $selected and build the view with them.在您的控制器中,您应该为$selected声明变量并使用它们构建视图。

Here's a more proper version of your template using blade syntax.这是使用刀片语法的更合适的模板版本。

<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>

And your controller:还有你的控制器:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM