B
B
bestxp2015-11-13 13:57:34
Twig
bestxp, 2015-11-13 13:57:34

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"

very strange behavior of composer.json
"twig/twig": "~1.23",
"ziadoz/silex-capsule": "1.*", -- service provider for Eloquent and Capsule/Manager
if you go deeper then Eloquent versions if you look at silex-capsule
"illuminate/database": "~5.0",
"illuminate/events": "~5.0"
it might be worth adding something or adjusting something for twig or Eloquent, but for now the problem remains
the same, which is the most interesting solution did not find for the Eloquent + Twig bundle

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
bestxp, 2015-11-13
@bestxp

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);
        }
    }

spied twig bridge for laravel

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question