我想显示多选中之前保存的数据。我有一个产品表,该产品表与促销详细信息表一对多连接,并且我想显示以前选择的产品,然后用户可以根据需要进行编辑。 这是我的产品型号
protected $table = 'm_product';
protected $primaryKey ='id';
protected $fillable = ['kode','id_kategori', 'foto_product', 'product', 'stok', 'stok_opname', 'ppn', 'harga', 'deskripsi', 'created_by', 'created_at', 'changed_by', 'updated_at', 'delete'];
public $timestamps = true;
public function kategori() {
return $this->belongsTo('App\Kategori', 'id_kategori');
}
public function stok() {
return $this->hasMany('App\Stok', 'id');
}
public function promodetail() {
return $this->hasMany('App\DetailPromo', 'id');
}
这是我的促销详情模型
protected $table = 'tb_promo_detail';
protected $primaryKey ='id';
protected $fillable = ['id_promo','id_kategori', 'id_product', 'det_potongan', 'det_jumlah_potongan'];
public $timestamps = false;
public function promo() {
return $this->belongsTo('App\Promo', 'id_promo');
}
public function produk() {
return $this->belongsTo('App\Produk', 'id_product');
}
这是我的blade.php
<div class="form-group row " id="inputbarang">
<label class="col-form-label col-lg-3 col-sm-12">Barang</label>
<div class="col-lg-4 col-md-9 col-sm-12">
<select class="form-control m-select2" width="500px" id="kt_select2_1" name="id_product[]" multiple="multiple">
<option value=""></option>
@foreach($produk as $p)
<option value="{{ $p->id }}" {{ $p->id == $detpromo->id_product ? 'selected' : '' }}>{{ $p->product }}</option>
@endforeach
</select>
</div>
</div>
这是我的控制器
$promo = Promo::find($id);
$detpromo = DetailPromo::where('id_promo', $id)->pluck('id_product')->toArray();
$produk = Produk::where('m_product.delete', 0)
->where('id_kategori', 1)
->orWhere('id_kategori', 2)
->orWhere('id_kategori', 3)->get();
$kategori = Kategori::where('delete', 0)->pluck('id');
return view('promoharirayaedit', ['promo' => $promo, 'detpromo' => $detpromo, 'produk' => $produk, 'kategori' => $kategori]);
我不知道该如何解决,请帮忙。。谢谢您!