Unable to access Eloquent attributes on Twig

I am trying to access the Eloquent attribute using Twig in Slim and get an error message.

I have a Field and Type object, and the ratio is as follows

class Field extends \Illuminate\Database\Eloquent\Model {

protected $table = 'fields';

public function type()
{
    return $this->belongsTo('models\Type');
}

When executed {{ f }}(being an f field), the output is as follows:

{"field_id":"1","field_name":"Your name","form_id":"2","type_id":"1","placeholder":"Please give us your name"}

And when executed, the {{ f.type }}result:

Message. An exception was thrown during template rendering ("An object of class Illuminate \ Database \ Eloquent \ Relations \ BelongsTo cannot be converted to a string") in "pages / editform.html" on line 97.

If I try to execute {{ f.type.name }}, it does not throw an exception but prints nothing.

If I do it in PHP

    $fields = $form->fields;
    var_dump($fields[0]->type->name);

The value is correctly displayed.

Any ideas ?, thanks

+4
2

. , , .

:

Field::with('type')->get()

- .

{{ f.type }}

. : http://laravel.com/docs/4.2/eloquent#eager-loading

+5

, Eloquent " " ( , f.type) , Twig "" .

Twig:

foo.bar PHP:

  • , foo - , bar - ;
  • , foo , , bar ;
  • , foo - , , bar ( bar - __construct());
  • , foo - , , getBar ;
  • , foo - , , isBar ;
  • , .

foo['bar'], , PHP:

  • , foo - , bar - ;
  • , .

- , : ", ". , PHP Twig isset $f->type. Eloquent __isset Model, , .

, __isset :

/**
 * Determine if an attribute exists on the model.
 *
 * @param  string  $key
 * @return bool
 */
public function __isset($key)
{
    return (isset($this->attributes[$key]) || isset($this->relations[$key])) ||
            ($this->hasGetMutator($key) && ! is_null($this->getAttributeValue($key)));
}

, "" (isset($this->relations[$key])). , type , Eloquent , "".

, Twig $f->type, , type :

... foo , , bar

type(), . ? type() () Relation ( , a BelongsTo), type. BelongsTo .

, Twig , $f->type , :

  • type Field, @roger-collins, ;
  • __isset Field:

public function __isset($name) { if (in_array($name, [ 'type' ])) { return true; } else { return parent::__isset($name); } }

Twig , type Field, , .

+7

All Articles