douchan6512 2019-07-17 03:08
浏览 355

Laravel多选复选框,用于多对多关系(crud)

After making the many-to-many relationship in my database I now moved on to try and implement the changes to the project itself. I am having a hard time doing this.

Essentially what I want is to add the multi-choice checkbox functionality to my Edit and Create to already set-up blade.php pages.

My Product Model (keep in mind the 'shipping_id' is the thing I want replaced with the multi-choice checkbox):

    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');
    }
}

My Country Model:

    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');
    }

    }

This is my Admin Product Controller:

    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'));
    }

And finally my create.blade.php for product:

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

  • 写回答

1条回答 默认 最新

  • douzhixun8393 2019-07-17 03:24
    关注

    With using multi select library in select tag add multiple attribute and make it as array in name

     <select multiple="multiple" id="my-select" name="shipping_id[]">
          <option value='elem_1'>elem 1</option>
          <option value='elem_2'>elem 2</option>
        </select>
    

    then in your controller save the data as json and you can use $casts so in your migration

    $table->text('shipping_id');
    

    in your model

    protected $casts =['shipping_id'=>'array']
    

    check casting in documentation https://laravel.com/docs/5.8/eloquent-mutators#attribute-casting .. so when save value will be automatically serialized "json_encode"

    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!