在数据库中建立了多对多关系后,我现在继续尝试对项目本身进行更改。我很难做到这一点。
基本上,我想要将多选复选框功能添加到我的“编辑和创建”中,以设置已经存在的blade.php页面。
我的产品模型(请记住,“ shipping_id”是我要用“多项选择”复选框替换的东西):
class Product extends Model
{
//
protected $fillable = [
'category_id',
'location_id',
'shipping_id',
'photo_id',
'title',
'body',
'price',
'availability',
'quantity'
];
public static $rules = array(
'category_id'=>'required|integer',
'location_id'=>'required|integer',
'shipping_id'=>'required|integer',
'title'=>'required|min:2',
'body'=>'required|min:20',
'price'=>'required|numeric',
'availability'=>'integer|between:0,1',
'quantity'=>'integer|between:1,99'
);
public function user(){
return $this->belongsTo('App\User');
}
public function photo(){
return $this->belongsTo('App\Photo');
}
public function category(){
return $this->belongsTo('App\Category');
}
public function location(){
return $this->belongsTo('App\Country');
}
public function shipping(){
return $this->belongsTo('App\Country');
}
public function comments(){
return $this->hasMany('App\Comment');
}
//experimental shipping
public function countries(){
return $this->belongsToMany('App\Country');
}
}
我的国家/地区模型:
class Country extends Model
{
//
protected $fillable = ['name'];
public static $rules = array('name'=>'required|min:3');
//experimental shipping
public function products(){
return $this->belongsToMany('App\Product');
}
}
这是我的管理产品控制器:
public function createEntry(ProductsCreateRequest $request)
{
//
$input = $request->all();
$user = Auth::user();
$validator = Validator::make(Input::all(), Product::$rules);
if($file = $request->file('photo_id')){
$name = time() . $file->getClientOriginalName();
$file->move('images', $name);
$photo = Photo::create(['file'=>$name]);
$input['photo_id'] = $photo->id;
}
if($validator->passes()){
$user->products()->create($input);
$product->countries()->sync(Input::get('countries'));
Session::flash('created_listing','The listing has been created');
return redirect('/admin/products');
}
return Redirect::to('admin/products/create')->withErrors($validator)-
>withInput();
}
public function editEntry($id)
{
//
$product = Product::findOrFail($id);
$categories = Category::pluck('name', 'id')->all();
$countries = Country::pluck('name', 'id')->all();
return view('admin.products.edit', compact('product', 'categories',
'countries'));
}
最后是我的create.blade.php产品:
```<h1>Create Products</h1>
<div class="row">
{!! Form::open(['method'=>'POST', 'action'=>
'AdminProductsController@store','files'=>true]) !!}
<div class="form-group">
{!! Form::label('title', 'Title:') !!}
{!! Form::text('title', null, ['class'=>'form-control'])!!}
</div>
<div class="form-group">
{!! Form::label('location_id', 'Item location:') !!}
{!! Form::select('location_id', [''=>'Choose Country'] +
$countries, null, ['class'=>'form-control'])!!}
</div>
<div class="form-group">
<label class="control-label">Shipping to:</label>
<select name="countries" id="countries" class="form-control" multiple="multiple">
@foreach($countries as $country)
<option value="{{$country->id}}">
{{$country->name}}
</option>
@endforeach
</select>
</div>
<div class="form-group">
{!! Form::label('shipping_id', 'Shipping to:') !!}
{!! Form::select('shipping_id', [''=>'Choose Country'] +
$countries, null, ['class'=>'form-control'])!!}
</div>
<div class="form-group">
{!! Form::label('category_id', 'Category:') !!}
{!! Form::select('category_id', [''=>'Choose Category'] +
$categories, null, ['class'=>'form-control'])!!}
</div>
<div class="form-group">
{!! Form::label('availability', 'Availability:') !!}
{!! Form::select('availability', ['1'=>'In Stock', '0'=>'Out Of
Stock'], null, ['class'=>'form-control'])!!}
</div>
<div class="form-group">
{!! Form::label('quantity', 'Quantity:') !!}
{!! Form::number('quantity', null, ['min'=>1,'max'=>99],
['class'=>'form-control'])!!}
</div>
<div class="form-group">
{!! Form::label('price', 'Price:') !!}
{!! Form::text('price', null, array('class'=>'form-price')) !!}
</div>
<div class="form-group">
{!! Form::label('photo_id', 'Photo:') !!}
{!! Form::file('photo_id', null, ['class'=>'form-control'])!!}
</div>
<div class="form-group">
{!! Form::label('body', 'Description:') !!}
{!! Form::textarea('body', null, ['class'=>'form-control'])!!}
</div>
<div class="form-group">
{!! Form::submit('Create Product', ['class'=>'btn btn-primary']) !!}
</div>
{!! Form::close() !!}
</div>
@include('includes.form_error')
My 3 tables are:
countries, products and country_product (with 'product_id' and 'country_id') - the relationship is already set and working.
In this state everything works.
So my question here is. How do I add the multi-choice checkbox functionality to the already set create and edit pages, so instead of 'shipping_id' in products table I can have an option for the user to select more than one countries the product can ship to?
P.S. I asked something similar yesterday (but it was related to the relationship only which I somehow managed to set-up now).
Thanks in advance!
答案 0 :(得分:0)
在选择标记中使用多重选择库 添加多个属性并将其设置为名称数组
<select multiple="multiple" id="my-select" name="shipping_id[]">
<option value='elem_1'>elem 1</option>
<option value='elem_2'>elem 2</option>
</select>
然后在您的控制器中 将数据另存为json,然后可以使用$ casts进行迁移
$table->text('shipping_id');
在您的模型中
protected $casts =['shipping_id'=>'array']
检查文档https://laravel.com/docs/5.8/eloquent-mutators#attribute-casting中的转换 ..因此,当保存值时,将自动序列化“ json_encode”