Answer the question
In order to leave comments, you need to log in
Eloquent ORM and Twig error, who faced?
I use Elouqent + Twig in Silex and after receiving a record from the database, for example, Post::findOrNew(100);
and trying to display the data {{ post.status }} I get the problem below, who and how struggled with such crap?
An exception has been thrown during the rendering of a template ("Call to undefined method Illuminate\Database\Query\Builder::status()") in "@incidents/view.html.twig"
Answer the question
In order to leave comments, you need to log in
Yes, it happens that you spend a couple of days searching, and then the chest opens, you just
need to redefine the base class for templates and redefine the method there
/**
* {@inheritdoc}
*/
protected function getAttribute(
$object,
$item,
array $arguments = [],
$type = Twig_Template::ANY_CALL,
$isDefinedTest = false,
$ignoreStrictCheck = false
)
{
// We need to handle accessing attributes on an Eloquent instance differently
if (Twig_Template::METHOD_CALL !== $type and is_a($object, 'Illuminate\Database\Eloquent\Model')) {
// We can't easily find out if an attribute actually exists, so return true
if ($isDefinedTest) {
return true;
}
// Call the attribute, the Model object does the rest of the magic
return $object->$item;
} else {
return parent::getAttribute($object, $item, $arguments, $type, $isDefinedTest, $ignoreStrictCheck);
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question