Laravel 5.0: Form::select() called twice Eloquent Accessor by select name

484 views Asked by At

Blade template:

{!! Form::model($category) !!}

    {!! Form::select('drinks_id', [...full list...]) !!}

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

'drinks_id' called by Eloquent Accessor:

public function getDrinksIdAttribute()
{
    var_dump('get');
    return 123;
}

When Form::select('drinks_id') execute, getDrinksIdAttribute() called twice and print string(3) "get" string(3) "get" from var_dump().

If I write this:

{!! Form::model($category) !!}

    {!! var_dump($category->drinks_id) !!}

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

it called getDrinksIdAttribute() once.

This is Form::select() bug, or I do something wrong?

1

There are 1 answers

0
Modder On BEST ANSWER

FormBuilder use object_get() helper function for get value from model:

/**
 * Get the model value that should be assigned to the field.
 *
 * @param  string  $name
 * @return string
 */
protected function getModelValueAttribute($name)
{
    if (is_object($this->model))
    {
        return object_get($this->model, $this->transformKey($name));
    }
    elseif (is_array($this->model))
    {
        return array_get($this->model, $this->transformKey($name));
    }
}

object_get() called Eloquent Accessor twice:

object_get() helper function code


The solution:

{!! Form::model($category) !!}

    {!! Form::select('drinks_id', [...full list...], $category->drinks_id) !!}

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