2015-06-17 9 views
1

刀片模板:Laravel 5.0:表::選擇()所選擇的名字叫兩聲雄辯訪問者

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

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

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

'drinks_id'通過雄辯訪問者稱爲:

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

Form::select('drinks_id')執行getDrinksIdAttribute()調用兩次並從打印。

如果我這樣寫:

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

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

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

getDrinksIdAttribute()一次。

這是Form::select()錯誤,或者我做錯了什麼?從模型獲取價值

回答

1

FormBuilder使用object_get()輔助功能:

/** 
* 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()叫雄辯訪問者兩次:

object_get() helper function code


解決辦法:

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

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

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