/**
* Fill a model with input data
*
* @param \Illuminate\Database\Eloquent\Model $model
* @param mixed $input
*
* @return array
*/
public function fillModel(&$model, $input)
{
$input = $input ? explode(',', $input) : array();
$fieldName = $this->getOption('field_name');
$relationship = $model->{$fieldName}();
//if this field is sortable, delete all the old records and insert the new ones one at a time
if ($sortField = $this->getOption('sort_field'))
{
//first delete all the old records
$relationship->delete();
//then re-attach them in the correct order
foreach ($input as $i => $item)
{
$relationship->attach($item, array($sortField => $i));
}
}
else
{
//elsewise the order doesn't matter, so use sync
$relationship->sync($input);
}
//unset the attribute on the model
$model->__unset($fieldName);
}