lundi 23 novembre 2015

How to populate select from Model with Laravel Collective FormBuilder

I'm using Laravel Collective FormBuilder version is 5.1

My Timeline model has a morphToMany with Brand model

So I have edit form for Timeline like this:

{!! Form::model($timeline, ['method' => 'PATCH', 'url' => 'admin/timeline/' . $timeline->id, 'files' => true]) !!}
    <div class="form-group">
        {!! Form::label('title', 'Title:') !!}
        {!! Form::text('title', null, ['class' => 'form-control']) !!}
    </div>
    <div class="form-group">
        {!! Form::label('brand_list', 'Brands:') !!}
        {!! Form::select('brand_list[]', App\Brand::lists('name', 'id'), null, ['id' => 'brand_list', 'class' => 'select2 form-control', 'multiple']) !!}
    </div>
{!! Form::close() !!}

in edit page's select box is empty but I have a relation when I was checking in MySql.

So I decided to check whats coming to FormBuilder.

My trace result:

public function getValueAttribute($name, $value = null)
{
  if (is_null($name)) {
      return $value;
  }

  if (!is_null($this->old($name))) {
      return $this->old($name);
  }

  if (!is_null($value)) {
      return $value;
  }

  if (isset($this->model)) {
      debug($this->getModelValueAttribute($name)); // Using helper for debuging
      return $this->getModelValueAttribute($name);
  }
}

getModelValueAttribute function is returning Collection

Illuminate\Support\Collection {
    #items: array:1 [
       0 => "1"
    ]
}

but in the getSelectedValue function its should be an array.

Any suggestion why it's not populating or how can I populate without any modification FormBuilder?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire