aurelijus.useckas16377
aurelijus.useckas16377

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

pratyushpundir6424
pratyushpundir6424

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

aurelijus.useckas16377
aurelijus.useckas16377

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
daftspunk
{% for sport in sports %} {{ sport.products_count.count }} {% endfor %}
aurelijus.useckas16377
aurelijus.useckas16377

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 :-/

daftspunk
daftspunk

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.

aurelijus.useckas16377
aurelijus.useckas16377

got it, thnx. Btw loving October. Good work!

chris10207
chris10207

and how to use in a php class ? doing this is rising an exception


$relations_count->count````
pratyushpundir6424
pratyushpundir6424

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)

pratyushpundir6424
pratyushpundir6424

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)

dev23124
dev23124

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

chris.e.mcquilkin11830
chris.e.mcquilkin11830

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
Mennax
Mennax

Does it have to do with: protected $jsonable = ['...']; ?

adc_91
adc_91

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

cikyelderly
cikyelderly

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 :-/

mmarfil13840
mmarfil13840

{% for sport in sports %} {{ sport.products_count[0].count }} {% endfor %}

1-16 of 16