php


How to add count to a eloquent array?


In my webshop i have searchgroups and searchtags.
Every searchtag belongs to a searchgroup.
The searchtags are connected to bikegroups with a many-to-many relation.
DB:
How i retrieve my groups with tags:
$searchgroups = SearchGroup::with('searchtags')->get();
Now i want to count how many times each searchtag exists in tags_bike_groups and display it.
So that i can display:
Year
2016 (2)
2017 (5)
2018 (0)
How do i achieve this?
I got it.
The solution is:
$searchgroups = SearchGroup::with('searchtags')->get();
foreach($searchgroups as $searchgroup) {
foreach($searchgroup->searchtags as $searchtag) {
$searchtag->count = count(TagsBikeGroup::where('searchtags_id',$searchtag->id)->get());
}
}
Then in your view you can get the count like this:
{{ $searchtag->count }}
Try this
$searchgroups = SearchGroup::with('searchtags')->get();

Related Links

Error in Module Loader
why is insert into not working
mysqli_query() won't work [duplicate]
Get xml results where element node is equal to string
How display images on each iteration (ajax)
stream_context_create with HTTP Option based on domain name
php skip a variable when calling a function [duplicate]
Wordpress Ajax returns -1
Show only rows that contain data in one specific column [duplicate]
Can't insert query result into table using PHP
Checking validity url in php [closed]
Stripe - retrieve Customer ID using email from form
How to Implement facebook login/registration in a REST web application
CakePHP 2.0 says that email is invalid
How to use weather API to create my weather HTML page [closed]
iOS security sending data with password to and from server

Categories

HOME
jms
key
dry
code-formatting
ios-simulator
iverilog
autocomplete
twitter-fabric
flurry
facebook-android-sdk
specifications
cocotb
swarm
elastic-load-balancer
driver
csrf
ex
distributed-computing
max-msp-jitter
asciimath
cocos2d-android
home
spring-shell
angular2-highcharts
quantitative-finance
plyr
beego
background-color
locks
dotcms
toad
quick-nimble
rust-cargo
caesar-cipher
cloudera-manager
adminer
android-canvas
char-pointer
twilio-php
installanywhere
cartopy
sspi
selenide
reset
resampling
optionaldataexception
xcode8.2
.net-micro-framework
ssi
roracle
iptv
algebra
test-data
kendo-editor
polymaps
google-shopping-api
qtextedit
controllers
deviare
metabase
livereload
qpixmap
android-syncadapter
xcopy
jnlp
mathematical-expressions
mutators
jolie
fasterxml
famo.us
symja
inequality
qt5.4
xcode6.3.1
mfmessagecomposeview
commenting
nosql-aggregation
apache-shindig
frontbase
grails-2.3
codahale-metrics
hints
insertion
bcdedit
ui-design
examine
ip-geolocation
word-frequency
gamesalad
navigationcontroller
data-dump
representation
page-curl
twitter-feed
html-generation
rootkit

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App