Volt count relationship

I would like to know how to call the count function in volt?

$robot = Robots::findFirst(2); echo "The robot has ", $robot->countRobotsParts(), " parts\n";

But when i try {{ robot.countRobotsParts() }} it's not working? How can I achieve this ?



27.4k

Hi,

you must pass $robot to the view ? If yes, that's strange ..., do you get any relevant error message ?

Note: Usually it is considered to be best pratice to prepare all you data in the controller context and then pass them to the view.



671
edited 7d ago

What do you get if you dump the robotparts? Does it return anything?

If I set view->disable() and make a foreach, and call the $robot->countRobotsParts(), it works perfect.

But if I pass the virable to the view, and in volt call robot.countRobotsParts() it dosent work?



671

Can you paste your code? Is a bit easier to help if its possible to see the actual code (controller action / .volt)

$this->hasMany('id', NAMESPACE.'\BlogsPostsCommentsReplies', 'commentid', array( 'alias' => 'replies', 'params' => [ 'order' => 'id', 'limit' => 12 ] ));

Then in my controller:

    $comments = BlogsPostsComments::find();
    $this->view->comments = $comments;

And finally in my View:

{% for comment in comments %}
    {{ comments.countReplies() }}
{% endfor %}

But it is not working?



671
edited 6d ago

Theres a typo in your .volt comments. should be comment. as below.

{% for comment in comments %}
    {{ comment.countReplies() }}
{% endfor %}
edited 6d ago

Oh god, thanks!

It's working fine now!



27.4k

You may accept Videles answer so that thread appears as solved