October: Count relationship display bug

Created on 25 Jun 2016  Â·  4Comments  Â·  Source: octobercms/october

I'm trying to setup the count relationship, 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 recreated it in sterile plugin/theme. Model "Sports" has a relationship with "Products" setup: 'products_count' => ['Newcode\Counttest\Models\Product', 'count' => true]

in controller:
$this['sports'] = Sports::all();
and in twig:{% for sport in sports %} ({{ sport.products_count }}) {% endfor %}

The result is
basketball ([]) football ([{"sports_id":"2","count":"5"}])

but I am not able to access the count variable with a dot as expected. The result is 1 (if exists and 0 if none)

I'm herewith attaching a test template/plugin.

count_bug.zip

Medium Archived Review Needed Unconfirmed Bug

Most helpful comment

the way to solve it is by putting zero before the variable to be accessed, ie: {{ sport.products_count.0.count }}

All 4 comments

I have noticed just now that same behavior is observed in more relation cases in general

the way to solve it is by putting zero before the variable to be accessed, ie: {{ sport.products_count.0.count }}

I had exactly the same problem, the 0 fixed it partially.(in my case the product was called follow)
If the count is 0 you get an error.
and echoing $this->product_count on the model which has hasMany[['MODEL]' , count=> true] defined I get
Collection {#999 â–¼
#items: []
}

If there is 1 or more product
Collection {#1002 â–¼
#items: array:1 [â–¼
0 => Follow {#1005 â–¼
+table: "hot_label_follows"
#fillable: array:2 [â–¶]
+belongsTo: array:2 [â–¶]
+implement: null
+attributes: array:2 [â–¶]
#jsonable: []
#dates: []
+hasMany: []
+hasOne: []
+belongsToMany: []
+morphTo: []
+morphOne: []
+morphMany: []
+morphToMany: []
+morphedByMany: []
+attachOne: []
+attachMany: []
+hasManyThrough: []
#connection: null
#primaryKey: "id"
#perPage: 15
+incrementing: true
+timestamps: true
#original: array:2 [â–¼
"label_id" => 1
"count" => 1
]
#relations: []
#hidden: []
#visible: []
#appends: []
#guarded: array:1 [â–¼
0 => "*"
]
#dateFormat: null
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
+wasRecentlyCreated: false
#emitterSingleEventCollection: []
#emitterEventCollection: []
#emitterEventSorted: []
#extensionData: array:3 [â–¶]
+sessionKey: null
#deferredBindingCache: null
}
]

which shows the count is indeed present under #original

Unfortunately i don't have time to check to the october source on what's going wrong here

Closing as it has been over a month since any activity on this occurred.

Was this page helpful?
0 / 5 - 0 ratings