无法访问Twig上的优秀属性 [英] Cannot access Eloquent attributes on Twig
问题描述
我有一个Field和一个Type对象,关系是遵循
class Field extends \Illuminate\Database\Eloquent\Model {
protected $ table ='fields';
public function type()
{
return $ this-> belongsTo('models\Type');
}
当执行 {{f}}
(为fa字段),输出为:
{field_id:1,field_name :你的名字,form_id:2,type_id:1,占位符:请给我们你的名字}
当做 {{f.type}}
时,结果是:
消息:渲染模板时抛出异常(对象类Illuminate\Database\Eloquent\Relations\BelongsTo无法转换为字符串)在第97行的pages / editform.html中。
如果我尝试执行 {f.type.name}}
,不会引发异常,但也不会打印任何内容。
如果我这样做在PHP中
$ fields = $ form-> fields;
var_dump($ fields [0] - > type-> name);
该值正确输出。
任何想法?
谢谢
我有同样的问题,并绊倒了这个问题。在自己解决之后,我以为我会尽力帮助你。
尝试在模型上做一个Eager Load:
Field :: with('type') - > get()
这应该允许您执行以下操作,无其他问题。
{{f.type }}
在这里查看更多信息: http://laravel.com/docs/4.2/eloquent#eager载入
I am trying to access an Eloquent attribute with Twig in Slim, and getting an error.
I have a Field and a Type object, and the relationship is as follows
class Field extends \Illuminate\Database\Eloquent\Model {
protected $table = 'fields';
public function type()
{
return $this->belongsTo('models\Type');
}
When doing {{ f }}
(being f a field), the output is this:
{"field_id":"1","field_name":"Your name","form_id":"2","type_id":"1","placeholder":"Please give us your name"}
And when doing {{ f.type }}
, the result is:
Message: An exception has been thrown during the rendering of a template ("Object of class Illuminate\Database\Eloquent\Relations\BelongsTo could not be converted to string") in "pages/editform.html" at line 97.
If I try to do {{ f.type.name }}
, doesn't throw up an exception but doesn't print anything either.
If I do it in PHP
$fields = $form->fields;
var_dump($fields[0]->type->name);
The value gets output correctly.
Any ideas?, Thanks
I had the same issue and stumbled upon this question. After solving it myself, I thought I'd try to help you out.
Try doing a Eager Load on the model:
Field::with('type')->get()
This should allow you to do the following with no other issues.
{{ f.type }}
See more info here: http://laravel.com/docs/4.2/eloquent#eager-loading
这篇关于无法访问Twig上的优秀属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!