lundi 17 octobre 2016

Laravel 5.2: How do you model-bind form checkboxes?

So basically i want to edit a shoe model which has: Name, description, color and size. The size attribute has options (some checked others unchecked depending on what's set in DB) which allows for different sizes to be checked depending on availability etc. I also want to be able to edit (check and/or uncheck). I'm trying to do a form model binding but I don't seem to get it right.

Product model:

Class Product extends Model {
  public function sizes() {
    return  $this->hasMany('App\Size');
  }
}

Size Model:

Class Size extends Model {
  public function products() {
     return  $this->hasMany('App\Product');
  }
}

Products Controller: edit action

Public function edit($id){
  $product = Product::find($id);
  return view('product.edit', ['product'=>$product]);
}

Now, the sizes table has men's sizes from size 6 to size 12 (i.e. 6,7,8,9,10,11,12). And i want all those options to show but display as 'checked' only the ones associated with this particular model. So if this model (shoe) has size 9 and 12 associated with it... have 9 and 12 options display 'checked' accordingly and the rest remain unchecked.

Edit View:

{!! Form::model($product, ['route'=>['product.update', $product->id], 'class'=>'form']) !!}

{!! Form::text('name') !!}
{!! Form::text('description') !!}
{!! Form::text('color') !!}

{!! Form::checkbox('size') !!}

{!! Form::submit('Submit') !!}

{!! Form::close() !!}

That's where i'm at. thanks




Aucun commentaire:

Enregistrer un commentaire