This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.
How should I be able to output the count of a relation? I set the models hasMany and I get the object like this: [{"size_id":"3","count":"2"}] unfortunately I'm not able to access it with a .count in twig... any suggestions? Thanks a lot
It will work just like you're trying to use it. Though I'm assuming you have it setup right:
Let's say you have a model called "Album" which hasMany "Songs" such that the relationship would be accessed in PHP like so:
$album = Album::find($id);
$songs = $album->songs();
If this works then in Twig you should be able to do:
<div>This album has {{ album.songs.count }} songs!!!</div>
Last updated
thnx for the input, but unfortunately it didn't help. I have everything setup right, but somehow the model relationship is displayed in json (square brackets and {} ), therefore twig is not able to access it with the "dot".
I have a "Sports" model relationship with "Products" setup:
'products_count' => ['Newcode\Product\Models\Product', 'count' => true]
in controller: $this['sports'] = Sports::all();
and in twig:
{% for sport in sports %} {{ sport.products_count }} {% endfor %}
The result is [{"size_id":"3","count":"2"}]
but I am not able to access the count variable because of square brackets. What am I doing wrong? :-/
daftspunk said:
{% for sport in sports %} {{ sport.products_count.count }} {% endfor %}
Thanx, I did exactly that, but I get wrong result. No matter what count field shows, I get 1 :-/
Hmm, I'm not sure. Your original data shows it should be 2. If you believe this to be a bug with October, create a sterile test case (test plugin and theme) and submit it to GitHub for testing.
and how to use in a php class ? doing this is rising an exception
$relations_count->count````
chris10207 said:
and how to use in a php class ? doing this is rising an exception
$relations_count->count````
You just be able to do count($record->relation)
chris10207 said:
and how to use in a php class ? doing this is rising an exception
$relations_count->count````
You just be able to do count($record->relation)
I think you should move the 'products_count' => ['Newcode\Product\Models\Product', 'count' => true] relation definition to hasOne
instead of hasMany
in Sports
model class. Something like this:
$hasOne = [
'products_count' => ['Newcode\Product\Models\Product', 'count' => true]
];
The hasMany relation provides a collection and that's why sports.products_count.count
doesn't work in twig. If you define the count relation in $hasOne
you should get a model object with a "count" attribute and in this case sports.products_count.count
should work in twig.
In addition, $this->products_count->count
should also work in Sports
model class.
Last updated
i know this was 11 months ago but to show the correct count do this
$model->relation_count[0]->count
for twig
model.relation_count[0].count
chris.e.mcquilkin11830 said:
i know this was 11 months ago but to show the correct count do this
$model->relation_count[0]->count
for twig
model.relation_count[0].count
This was the only thing that worked for me. Thank you.
Last updated
aurelijus.useckas16377 said: mangafox
thnx for the input, but unfortunately it didn't help. I have everything setup right, but somehow the model relationship is displayed in json (square brackets and {} ), therefore twig is not able to access it with the "dot". I have a "Sports" model relationship with "Products" setup:
'products_count' => ['Newcode\Product\Models\Product', 'count' => true]
in controller:
$this['sports'] = Sports::all();
and in twig:{% for sport in sports %} {{ sport.products_count }} {% endfor %}
The result is [{"size_id":"3","count":"2"}]
but I am not able to access the count variable because of square brackets. What am I doing wrong? :-/
aurelijus.useckas16377 said:
thnx for the input, but unfortunately it didn't help. I have everything setup right, but somehow the model relationship is displayed in json (square brackets and {} ), therefore twig is not able to access it with the "dot". I have a "Sports" model relationship with "Products" setup:
'products_count' => ['Newcode\Product\Models\Product', 'count' => true]
in controller:
$this['sports'] = Sports::all();
and in twig:{% for sport in sports %} {{ sport.products_count }} {% endfor %}
The result is [{"size_id":"3","count":"2"}]
but I am not able to access the count variable because of square brackets. What am I doing wrong? :-/
Thanx, I did exactly that, but I get wrong result. No matter what count field shows, I get 1 :-/
1-16 of 16