Laravel 5.0:Form::select() 调用了两次 Eloquent 访问器 select 名称
Laravel 5.0: Form::select() called twice Eloquent Accessor by select name
Blade 模板:
{!! Form::model($category) !!}
{!! Form::select('drinks_id', [...full list...]) !!}
{!! Form::close() !!}
'drinks_id'
由 Eloquent 访问器调用:
public function getDrinksIdAttribute()
{
var_dump('get');
return 123;
}
当 Form::select('drinks_id')
执行时,getDrinksIdAttribute()
调用两次并从 var_dump()
.
打印 string(3) "get" string(3) "get"
如果我这样写:
{!! Form::model($category) !!}
{!! var_dump($category->drinks_id) !!}
{!! Form::close() !!}
它调用了 getDrinksIdAttribute()
一次。
这是 Form::select()
错误,还是我做错了什么?
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()
两次调用 Eloquent 访问器:
解决方法:
{!! Form::model($category) !!}
{!! Form::select('drinks_id', [...full list...], $category->drinks_id) !!}
{!! Form::close() !!}
Blade 模板:
{!! Form::model($category) !!}
{!! Form::select('drinks_id', [...full list...]) !!}
{!! Form::close() !!}
'drinks_id'
由 Eloquent 访问器调用:
public function getDrinksIdAttribute()
{
var_dump('get');
return 123;
}
当 Form::select('drinks_id')
执行时,getDrinksIdAttribute()
调用两次并从 var_dump()
.
string(3) "get" string(3) "get"
如果我这样写:
{!! Form::model($category) !!}
{!! var_dump($category->drinks_id) !!}
{!! Form::close() !!}
它调用了 getDrinksIdAttribute()
一次。
这是 Form::select()
错误,还是我做错了什么?
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()
两次调用 Eloquent 访问器:
解决方法:
{!! Form::model($category) !!}
{!! Form::select('drinks_id', [...full list...], $category->drinks_id) !!}
{!! Form::close() !!}