在返回和编辑产品时,如何将数据保留在我的选择框中?
这是包含父类和子类的表单:
<form role="form" method="POST" action="{{ route('admin.product.update', $product->id) }}">
{{ csrf_field() }}
<div class="col-xs-12 col-sm-6 col-md-6">
<div class="form-group{{ $errors->has('category') ? ' has-error' : '' }}">
<label>Parent Category</label>
<select class="form-control" name="category" id="category" data-url="{{ url('api/dropdown')}}">
<option value=""></option>
@foreach($categories as $category)
<option value="{{ $category->id }}">{{ $category->category }}</option>
@endforeach
</select>
@if($errors->has('category'))
<span class="help-block">{{ $errors->first('category') }}</span>
@endif
</div>
<br>
</div>
<div class="col-xs-12 col-sm-6 col-md-6">
<div class="form-group{{ $errors->has('cat_id') ? ' has-error' : '' }}">
<label>Sub-Category Category</label>
<select class="form-control" name="cat_id" id="sub_category">
<option value=""></option>
</select>
@if($errors->has('cat_id'))
<span class="help-block">{{ $errors->first('cat_id') }}</span>
@endif
</div>
<br>
</div>
<div class="form-group col-md-12">
<button type="submit" class="btn btn-primary waves-effect waves-light">Edit Product</button>
</div>
</form>
以下是我的功能,可以将结果编辑为:
/**
* Return the view to edit & Update the Products
*
* @param $id
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function editProduct($id) {
// Find the product ID
$product = Product::findOrFail($id);
// Get all Categories where parent_id = NULL
$categories = Category::whereNull('parent_id')->get();
// Return view with products and categories
return view('admin.product.edit', compact('product', 'categories'));
}
以下是我的类别和子类别的类别模型:
class Category extends Model {
protected $table = 'categories';
protected $fillable = ['category'];
/**
* One sub category, belongs to a Main Category ( Or Parent Category ).
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function parent() {
return $this->belongsTo('App\Category', 'parent_id');
}
/**
* A Parent Category has many sub categories
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function children() {
return $this->hasMany('App\Category', 'parent_id');
}
/**
* One Category can have many Products.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function product() {
return $this->hasMany('App\Product', 'id');
}
}
以下是我的产品表的产品型号:
class Product extends Model {
protected $table = 'products';
protected $fillable = [
'product_name',
'price',
'cat_id',
'featured',
'brand_id',
];
/**
* One Product can have one Category.
*
* @return \Illuminate\Database\Eloquent\Relations\HasOne
*/
public function category() {
return $this->hasOne('App\Category', 'id');
}
}
只是为了让您知道,当我选择父类别时,ajax调用将触发并检索所有父子类别。我没有把这个问题包括在内。
答案 0 :(得分:2)
对于创作表单:
@foreach($categories as $category)
<option value="{{ $category->id }}" @if(old('category')&&old('category')== $category->id) selected='selected' @endif >{{ $category->category }}</option>
@endforeach
对于编辑表单:
@foreach($categories as $category)
<option value="{{ $category->id }}" @if($category->id==$model->category) selected='selected' @endif >{{ $category->category }}</option>
@endforeach
答案 1 :(得分:0)
首先确保您的输入被重定向回表单:
return redirect('form')->withInput();
然后使用旧的输入数据检查密钥:
@foreach($categories as $category)
<option value="{{ $category->id }}" {{ ($category->id == old('category') ? 'selected="selected"' : '') }}>{{ $category->category }}</option>
@endforeach
答案 2 :(得分:0)
这是对@LewiG的回答的解释。
创建项目时,如果输入无效,我们会返回输入,在这种情况下,我们应准备视图以处理old($key)
输入。
因此,对于我们特定的<select></select>
标记,我们将选项与旧输入进行比较。
e.g。
@foreach($categories as $category)
<option value="{{ $category->id }}" @if(old('category')&&old('category')== $category->id) selected='selected' @endif >{{ $category->category }}</option>
@endforeach
当我们编辑项目时,例如具有类别的产品, 我们通过比较产品中的category_id属性(例如)来预先选择当前类别。 e.g。
@foreach($categories as $category)
<option value="{{ $category->id }}" @if($category->id==$product->category_id) selected='selected' @endif >{{ $category->category }}</option>
@endforeach
现在$ product或$ model或$来自哪里? 数据库,Eloquent,查询,或者只是您创建的对象 然后我们使用
将其返回到视图return view('product.edit')->withProduct($product);
或
return view('product.edit')->with('product',$product);
答案 3 :(得分:0)
进行此更改并尝试
<div class="form-group">
<label>Parent Category</label>
<select class="form-control" name="category" id="category" data-url="{{ url('api/dropdown')}}" >
<option value=""></option>
@foreach($categories as $category)
<option value="{{ $category->id }}" {{$product->Category->parent->id == $category->id ? "selected" : "" }}>{{ $category->category}}</option>
@endforeach
</select>
<br>
</div>