I have a output from dreamfactory ie using laravel.Follwing is the array response from the api .
DreamFactory\Core\Models\NonAdminUser Object
(
[table:protected] => user
[fillable:protected] => Array
(
[0] => name
[1] => username
[2] => first_name
[3] => last_name
[4] => email
[5] => is_active
[6] => phone
[7] => security_question
[8] => security_answer
[9] => adldap
[10] => oauth_provider
[11] => last_login_date
[12] => default_app_id
)
[rules:protected] => Array
(
[name] => required|max:255
[email] => required|email|max:255
[username] => min:6|unique:user,username|regex:/^\S*$/u|nullable
)
[appends:protected] => Array
(
[0] => confirmed
[1] => expired
)
[hidden:protected] => Array
(
[0] => is_sys_admin
[1] => password
[2] => remember_token
[3] => security_answer
)
[casts:protected] => Array
(
[is_active] => boolean
[is_sys_admin] => boolean
[id] => integer
)
[schemaExtension:protected] =>
[connection:protected] => mysql
[primaryKey:protected] => id
[keyType:protected] => int
[incrementing] => 1
[with:protected] => Array
(
)
[perPage:protected] => 15
[exists] => 1
[wasRecentlyCreated] =>
[attributes:protected] => Array
(
[id] => 61
[name] => test1 test1
[username] => test1#gmail.com
[first_name] => test1
[last_name] => test1
[last_login_date] => 2017-12-07 01:20:39
[email] => test1#gmail.com
[password] => $2y$10$xHxN8FR/kHGq6pSY4Xa1NOW9EwmUzgMH2oOmtkhWWkNqSxKMMUhdO
[is_sys_admin] => 0
[is_active] => 1
[phone] =>
[security_question] =>
[security_answer] =>
[confirm_code] => y
[default_app_id] =>
[remember_token] =>
[adldap] =>
[oauth_provider] =>
[saml] =>
[created_date] => 2017-11-27 07:23:56
[last_modified_date] => 2018-03-21 12:08:20
[created_by_id] => 1
[last_modified_by_id] => 1
)
[original:protected] => Array
(
[id] => 61
[name] => test1 test1
[username] => test1#gmail.com
[first_name] => test1
[last_name] => test1
[last_login_date] => 2017-12-07 01:20:39
[email] => test1#gmail.com
[password] => $2y$10$xHxN8FR/kHGq6pSY4Xa1NOW9EwmUzgMH2oOmtkhWWkNqSxKMMUhdO
[is_sys_admin] => 0
[is_active] => 1
[phone] =>
[security_question] =>
[security_answer] =>
[confirm_code] => y
[default_app_id] =>
[remember_token] =>
[adldap] =>
[oauth_provider] =>
[saml] =>
[created_date] => 2017-11-27 07:23:56
[last_modified_date] => 2018-03-21 12:08:20
[created_by_id] => 1
[last_modified_by_id] => 1
)
[dates:protected] => Array
(
)
[dateFormat:protected] =>
[events:protected] => Array
(
)
[observables:protected] => Array
(
)
[relations:protected] => Array
(
[user_lookup_by_user_id] => Illuminate\Database\Eloquent\Collection Object
(
[items:protected] => Array
(
[0] => DreamFactory\Core\Models\UserLookup Object
(
[table:protected] => user_lookup
[fillable:protected] => Array
(
[0] => user_id
[1] => name
[2] => value
[3] => private
[4] => description
)
[casts:protected] => Array
(
[id] => integer
[user_id] => integer
[private] => boolean
)
[encrypted:protected] => Array
(
[0] => value
)
[schemaExtension:protected] =>
[connection:protected] => mysql
[primaryKey:protected] => id
[keyType:protected] => int
[incrementing] => 1
[with:protected] => Array
(
)
[perPage:protected] => 15
[exists] => 1
[wasRecentlyCreated] =>
[attributes:protected] => Array
(
[id] => 5
[user_id] => 61
[name] => naga
[value] => eyJpdiI6ImZldTVMUzdMd0xZRGZ0TzlGdnpFTnc9PSIsInZhbHVlIjoiM0t5Y3pFMnh2czhxWjhDNk1CcjNkUT09IiwibWFjIjoiMTMwYjI0ZTY4MTA2MDk1MzgwMTIxMjYzY2U4MDE1MDMxMmY4NzI5M2IyMThhYmQwZWM1ZGU5YjNiYzM2MzJlMCJ9
[private] => 0
[description] =>
[created_date] => 2018-03-21 12:08:20
[last_modified_date] => 2018-03-21 12:08:20
[created_by_id] =>
[last_modified_by_id] =>
)
[original:protected] => Array
(
[id] => 5
[user_id] => 61
[name] => naga
[value] => eyJpdiI6ImZldTVMUzdMd0xZRGZ0TzlGdnpFTnc9PSIsInZhbHVlIjoiM0t5Y3pFMnh2czhxWjhDNk1CcjNkUT09IiwibWFjIjoiMTMwYjI0ZTY4MTA2MDk1MzgwMTIxMjYzY2U4MDE1MDMxMmY4NzI5M2IyMThhYmQwZWM1ZGU5YjNiYzM2MzJlMCJ9
[private] => 0
[description] =>
[created_date] => 2018-03-21 12:08:20
[last_modified_date] => 2018-03-21 12:08:20
[created_by_id] =>
[last_modified_by_id] =>
)
[dates:protected] => Array
(
)
[dateFormat:protected] =>
[appends:protected] => Array
(
)
[events:protected] => Array
(
)
[observables:protected] => Array
(
)
[relations:protected] => Array
(
)
[touches:protected] => Array
(
)
[timestamps] => 1
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
[cacheTTL:protected] => 0
[cachePrefix:protected] =>
[cacheKeys:protected] => Array
(
)
[protectedView] => 1
[protectionMask:protected] => **********
[protected:protected] => Array
(
)
[encryptedView] =>
[rules:protected] => Array
(
)
[validationMessages:protected] => Array
(
)
[errors:protected] => Array
(
)
)
)
)
)
[touches:protected] => Array
(
)
[timestamps] => 1
[visible:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
[cacheTTL:protected] => 0
[cachePrefix:protected] =>
[cacheKeys:protected] => Array
(
)
[protectedView] => 1
[protectionMask:protected] => **********
[protected:protected] => Array
(
)
[encryptedView] =>
[encrypted:protected] => Array
(
)
[validationMessages:protected] => Array
(
)
[errors:protected] => Array
(
)
[rememberTokenName:protected] => remember_token
How to parse values of user_lookup_by_user_id using php
Related
I have 2 master table
tbl_master_delivery_type
tbl_master_business_types
and 2 user table
tbl_users_business_sub_delivery_options
tbl_users_business
tbl_master_delivery_type tables - master tables
delivery_type_id (primary key)
delivery_type_name
created_by
updated_by
is_delete
status
deleted_at
created_at
updated_at
tbl_master_business_types - master tables
business_type_id (primary key)
business_type_name
description
created_by
updated_by
is_delete
status
deleted_at
created_at
updated_at
tbl_users_business - user table
business_id (primary key)
tbl_users_business_sub_delivery_options - user table
business_del_sub_option_id (primary key)
business_id (foreign key)
business_type_id (foreign key)
delivery_type_id (foreign key
deleted_at
created_at
updated_at
So I want to get data from tbl_users_business_sub_delivery_options table using relationship in laravel.
I have tried using hasMany relationship in function some as below code.
public function usersBusinessDeliveryTypesWeb()
{
return $this->hasMany('App\UsersBusinessSubDeliveryOption', 'business_id');
}
but I am getting the null value.
[usersBusinessDeliveryTypesWeb] => Illuminate\Database\Eloquent\Collection Object
(
[items:protected] => Array
(
)
)
But if I make the function using belongsTo.
public function usersBusinessDeliveryTypesWeb()
{
return $this->belongsTo('App\UsersBusinessSubDeliveryOption', 'business_id');
}
then I am getting only one value as given below.
[usersBusinessDeliveryTypesWeb] => App\UsersBusinessSubDeliveryOption Object
(
[primaryKey:protected] => business_del_sub_option_id
[table:protected] => tbl_users_business_sub_delivery_options
[fillable:protected] => Array
(
[0] => business_id
[1] => business_type_id
[2] => delivery_type_id
)
[hidden:protected] => Array
(
)
[dates] => Array
(
[0] => deleted_at
)
[connection:protected] => pgsql
[keyType:protected] => int
[incrementing] => 1
[with:protected] => Array
(
)
[withCount:protected] => Array
(
)
[perPage:protected] => 15
[exists] => 1
[wasRecentlyCreated] =>
[attributes:protected] => Array
(
[business_del_sub_option_id] => 1
[business_id] => 4
[business_type_id] => 3
[delivery_type_id] => 1
[deleted_at] =>
[created_at] =>
[updated_at] =>
)
[original:protected] => Array
(
[business_del_sub_option_id] => 1
[business_id] => 4
[business_type_id] => 3
[delivery_type_id] => 1
[deleted_at] =>
[created_at] =>
[updated_at] =>
)
[changes:protected] => Array
(
)
[casts:protected] => Array
(
)
[dateFormat:protected] =>
[appends:protected] => Array
(
)
[dispatchesEvents:protected] => Array
(
)
[observables:protected] => Array
(
)
[relations:protected] => Array
(
)
[touches:protected] => Array
(
)
[timestamps] => 1
[visible:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
[forceDeleting:protected] =>
)
But actually I want the output like this.
[usersBusinessDeliveryTypesWeb] => Illuminate\Database\Eloquent\Collection Object
(
[items:protected] => Array
(
[0] => App\UsersBusinessSubDeliveryOption Object
(
[primaryKey:protected] => business_del_sub_option_id
[table:protected] => tbl_users_business_sub_delivery_options
[fillable:protected] => Array
(
[0] => business_id
[1] => business_type_id
[2] => delivery_type_id
)
[hidden:protected] => Array
(
)
[dates] => Array
(
[0] => deleted_at
)
[connection:protected] => pgsql
[keyType:protected] => int
[incrementing] => 1
[with:protected] => Array
(
)
[withCount:protected] => Array
(
)
[perPage:protected] => 15
[exists] => 1
[wasRecentlyCreated] =>
[attributes:protected] => Array
(
[business_del_sub_option_id] => 1
[business_id] => 4
[business_type_id] => 3
[delivery_type_id] => 1
[deleted_at] =>
[created_at] =>
[updated_at] =>
)
[original:protected] => Array
(
[business_del_sub_option_id] => 1
[business_id] => 4
[business_type_id] => 3
[delivery_type_id] => 1
[deleted_at] =>
[created_at] =>
[updated_at] =>
)
[changes:protected] => Array
(
)
[casts:protected] => Array
(
)
[dateFormat:protected] =>
[appends:protected] => Array
(
)
[dispatchesEvents:protected] => Array
(
)
[observables:protected] => Array
(
)
[relations:protected] => Array
(
)
[touches:protected] => Array
(
)
[timestamps] => 1
[visible:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
[forceDeleting:protected] =>
)
[1] => App\UsersBusinessSubDeliveryOption Object
(
[primaryKey:protected] => business_del_sub_option_id
[table:protected] => tbl_users_business_sub_delivery_options
[fillable:protected] => Array
(
[0] => business_id
[1] => business_type_id
[2] => delivery_type_id
)
[hidden:protected] => Array
(
)
[dates] => Array
(
[0] => deleted_at
)
[connection:protected] => pgsql
[keyType:protected] => int
[incrementing] => 1
[with:protected] => Array
(
)
[withCount:protected] => Array
(
)
[perPage:protected] => 15
[exists] => 1
[wasRecentlyCreated] =>
[attributes:protected] => Array
(
[business_del_sub_option_id] => 1
[business_id] => 4
[business_type_id] => 3
[delivery_type_id] => 2
[deleted_at] =>
[created_at] =>
[updated_at] =>
)
[original:protected] => Array
(
[business_del_sub_option_id] => 1
[business_id] => 4
[business_type_id] => 3
[delivery_type_id] => 2
[deleted_at] =>
[created_at] =>
[updated_at] =>
)
[changes:protected] => Array
(
)
[casts:protected] => Array
(
)
[dateFormat:protected] =>
[appends:protected] => Array
(
)
[dispatchesEvents:protected] => Array
(
)
[observables:protected] => Array
(
)
[relations:protected] => Array
(
)
[touches:protected] => Array
(
)
[timestamps] => 1
[visible:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
[forceDeleting:protected] =>
)
)
hasOne -> returns one item
hasMany -> returns a collection
belongsTo -> returns one item
belongsToMany -> returns a collection. (works only with pivot tables)
Also, in your models, do you specify your primary key, cause you are not using the standard 'id' col for each table... instead you use names like: delivery_type_id.
i would, either Check that my models have the proper id set.
Or Use explicitly
return $this->hasMany('App\UsersBusinessSubDeliveryOption', 'business_id', 'business_id');
I have below array structure how to parse this array and to display all values of [hits][hits] in codeigniter. Can anyone help me with the code?
Array
(
[took] => 2
[timed_out] =>
[_shards] => Array
(
[total] => 5
[successful] => 5
[skipped] => 0
[failed] => 0
)
[hits] => Array
(
[total] => 211
[max_score] => 1
[hits] => Array
(
[0] => Array
(
[_index] => employee
[_type] => employee_details
[_id] => AULz9GEBLWX90hClBAhn
[_score] => 1
[_source] => Array
(
[Id] => 5
[First name] => c
[Last name] => d
[Email] => d#gmail.com
[Phone] => 12
[Address] => n
[Password] => HARI
[Status] => false
)
)
[1] => Array
(
[_index] => employee
[_type] => employee_details
[_id] => eM1j6mEB19ahvbtRhU6F
[_score] => 1
[_source] => Array
(
[Id] =>
[First name] =>
[Last name] =>
[Email] =>
[Phone] =>
[Address] =>
[Password] =>
[Status] =>
[createddate] =>
[Updateddate] =>
)
)
[2] => Array
(
[_index] => employee
[_type] => employee_details
[_id] => ec1j6mEB19ahvbtRhk5G
[_score] => 1
[_source] => Array
(
[Id] =>
[First name] =>
[Last name] =>
[Email] =>
[Phone] =>
[Address] =>
[Password] =>
[Status] =>
[createddate] =>
[Updateddate] =>
)
)
[3] => Array
(
[_index] => employee
[_type] => employee_details
[_id] => es1j6mEB19ahvbtRh04U
[_score] => 1
[_source] => Array
(
[Id] =>
[First name] =>
[Last name] =>
[Email] =>
[Phone] =>
[Address] =>
[Password] =>
[Status] =>
[createddate] =>
[Updateddate] =>
)
)
[4] => Array
(
[_index] => employee
[_type] => employee_details
[_id] => fM1j6mEB19ahvbtRiE6M
[_score] => 1
[_source] => Array
(
[Id] =>
[First name] =>
[Last name] =>
[Email] =>
[Phone] =>
[Address] =>
[Password] =>
[Status] =>
[createddate] =>
[Updateddate] =>
)
)
[5] => Array
(
[_index] => employee
[_type] => employee_details
[_id] => ds1j6mEB19ahvbtRg07Y
[_score] => 1
[_source] => Array
(
[Id] =>
[First name] =>
[Last name] =>
[Email] =>
[Phone] =>
[Address] =>
[Password] =>
[Status] =>
[createddate] =>
[Updateddate] =>
)
)
[6] => Array
(
[_index] => employee
[_type] => employee_details
[_id] => d81j6mEB19ahvbtRhE6q
[_score] => 1
[_source] => Array
(
[Id] =>
[First name] =>
[Last name] =>
[Email] =>
[Phone] =>
[Address] =>
[Password] =>
[Status] =>
[createddate] =>
[Updateddate] =>
)
)
[7] => Array
(
[_index] => employee
[_type] => employee_details
[_id] => hs1j6mEB19ahvbtRok5L
[_score] => 1
[_source] => Array
(
[Id] =>
[First name] =>
[Last name] =>
[Email] =>
[Phone] =>
[Address] =>
[Password] =>
[Status] =>
[createddate] =>
[Updateddate] =>
)
)
[8] => Array
(
[_index] => employee
[_type] => employee_details
[_id] => iM1j6mEB19ahvbtRpU41
[_score] => 1
[_source] => Array
(
[Id] =>
[First name] =>
[Last name] =>
[Email] =>
[Phone] =>
[Address] =>
[Password] =>
[Status] =>
[createddate] =>
[Updateddate] =>
)
)
[9] => Array
(
[_index] => employee
[_type] => employee_details
[_id] => cM1U6mEB19ahvbtRNk4F
[_score] => 1
[_source] => Array
(
[Id] =>
[First name] =>
[Last name] =>
[Email] =>
[Phone] =>
[Address] =>
[Password] =>
[Status] =>
[createddate] =>
[Updateddate] =>
)
)
)
)
)
Can anyone help me to parse the above array to display the id,first_name,last_name,Email,Password,Address,Status,Createddate,Updateddate all the details in html form?
Use foreach for fetching each element from the array as follows:
Assign the above array to an array variable(i.e here Im taking as $result).
foreach($result['hits']['hits'] as $each)
{
echo $each['_source']['id'];
echo $each['_source']['first_name'];
echo $each['_source']['last_name'];
echo $each['_source']['Email'];
echo $each['_source']['Password'];
echo $each['_source']['Address'];
echo $each['_source']['Status'];
echo $each['_source']['Createddate'];
echo $each['_source']['Updateddate'];
}
I have a multi-dimentional Laravel collection called transactions. When output as an array, it looks like this:
print_r(transactions->toArray())
Array
(
[0] => Array
(
[id] => 2
[name] => Level 1
[subcategories] => Array
(
[0] => Array
(
[id] => 3
[name] => Level 2
[transaction_reference] => Array
(
[0] => Array
(
[id] => 1
[name] => Level 3
[children] => Array
(
[id] => 4
[name] => Level 4
)
)
[1] => Array
(
[id] => 2
[name] => Level 3
[children] => Array
(
[id] => 5
[name] => Level 4
)
)
)
)
)
)
)
At level 2, there is an array called "transaction_reference" which contains two elements - each of which contain one more element. I need these two elements to be merged so transaction_reference contains only one element which in turn contains two elements.
In other words, I need the result to resemble the following:
Array
(
[0] => Array
(
[id] => 2
[name] => Level 1
[subcategories] => Array
(
[0] => Array
(
[id] => 3
[name] => Level 2
[transaction_reference] => Array
(
[0] => Array
(
[id] => 1
[name] => Level 3
[children] => Array
(
[id] => 4
[name] => Level 4
)
(
[id] => 5
[name] => Level 4
)
)
)
)
)
)
)
I have tried using flatten() in a variety of formats, but cannot seem to get what I require.
Is this possible?
Thanks
My query is returning the wrong results and I think it is because the final parameter (minutes) is not being binded to the query. when I get the querylog everything seems fine but the wrong results are being returned. If I putthe minutes parameter directly into the query it returns the right results as expected but this value needs to be a variable.
To explain the query it is counting all records until the total minutes hit a set number, in my example im using 500.
This query works and returns results as expected:
DB::select('SELECT NULL AS session_total_charges, NULL AS call_date, NULL AS inbound_duration, NULL AS total
FROM dual
WHERE #total := 0
UNION ALL
SELECT session_total_charges, call_date, inbound_duration, #total := #total + inbound_duration AS total
FROM (SELECT * FROM records ORDER BY call_date) C where calling_user =:user and call_date LIKE :date AND outbound_zone_id IN ("UKX","UKM","UKLR","UKNR") and #total < 500', ["user"=>$user, "date"=>$date]);
This query does not work and only returns one row
DB::select('SELECT NULL AS session_total_charges, NULL AS call_date, NULL AS inbound_duration, NULL AS total
FROM dual
WHERE #total := 0
UNION ALL
SELECT session_total_charges, call_date, inbound_duration, #total := #total + inbound_duration AS total
FROM (SELECT * FROM records ORDER BY call_date) C where calling_user =:user and call_date LIKE :date AND outbound_zone_id IN ("UKX","UKM","UKLR","UKNR") and #total < :minutes', ["user"=>$user, "date"=>$date, "minutes"=>$minutes]);
[query] => SELECT NULL AS session_total_charges, NULL AS call_date, NULL AS inbound_duration, NULL AS total
FROM dual
WHERE #total := 0
UNION ALL
SELECT session_total_charges, call_date, inbound_duration, #total := #total + inbound_duration AS total
FROM (SELECT * FROM records ORDER BY call_date) C where calling_user =:user and call_date LIKE :date AND outbound_zone_id IN ("UKX","UKM","UKLR","UKNR") and #total < :minutes
[bindings] => Array
(
[user] => T-M000005251-009
[minutes] => 500
[date] => 2016-12-%%
)
//Result using bindings (only one row returned)
[0] => stdClass Object
(
[session_total_charges] => 0.014125
[call_date] => 2016-12-01 09:12:39
[inbound_duration] => 113
[total] => 113
)
//Result with values inserted directly into query(correct result returned)
[0] => stdClass Object
(
[session_total_charges] => 0.014125
[call_date] => 2016-12-01 09:12:39
[inbound_duration] => 113
[total] => 113
)
[1] => stdClass Object
(
[session_total_charges] => 0.04733333
[call_date] => 2016-12-01 09:18:16
[inbound_duration] => 142
[total] => 255
)
[2] => stdClass Object
(
[session_total_charges] => 0.03866667
[call_date] => 2016-12-01 09:22:21
[inbound_duration] => 116
[total] => 371
)
[3] => stdClass Object
(
[session_total_charges] => 0.012625
[call_date] => 2016-12-01 09:29:24
[inbound_duration] => 101
[total] => 472
)
[4] => stdClass Object
(
[session_total_charges] => 0.0505
[call_date] => 2016-12-01 12:03:16
[inbound_duration] => 404
[total] => 876
)
I have been struggling on how to get the quantity of distinct values from a collection in Eloquent.
I have been trying several methods, such as unique(), values(), etc., found on the docs. Even though there is indeed a count() method, there is no method to get the count of distinct values.
For example, by applying the following query
$technicalshighestdegrees = Capsule::table('academicinfo AS fa')
->selectRaw('DISTINCT fa.academic_id AS Id,c.name AS Degree')
->leftJoin('academics AS a','fa.academic_id','=','a.id')
->leftJoin('cat_degree AS c','fa.level','=','c.id')
->whereIn('a.type',['Technical'])
->where('a.status','!=','Retired')
->where('c.degree',true)
->orderBy('a.id')
->orderBy('c.hierarchy','desc')/*http://stackoverflow.com/a/17006377/1883256*/
->get();
I get this collection:
Illuminate\Support\Collection Object
(
[items:protected] => Array
(
[0] => stdClass Object
(
[Id] => 3
[Grado] => Master
)
[1] => stdClass Object
(
[Id] => 3
[Grado] => Bachelor
)
[2] => stdClass Object
(
[Id] => 4
[Grado] => Master
)
[3] => stdClass Object
(
[Id] => 4
[Grado] => Bachelor
)
[4] => stdClass Object
(
[Id] => 6
[Grado] => Master
)
[5] => stdClass Object
(
[Id] => 6
[Grado] => Bachelor
)
[6] => stdClass Object
(
[Id] => 18
[Grado] => Bachelor
)
[7] => stdClass Object
(
[Id] => 27
[Grado] => Bachelor
)
[8] => stdClass Object
(
[Id] => 34
[Grado] => Master
)
[9] => stdClass Object
(
[Id] => 34
[Grado] => Bachelor
)
[10] => stdClass Object
(
[Id] => 36
[Grado] => PhD
)
[11] => stdClass Object
(
[Id] => 36
[Grado] => Master
)
[12] => stdClass Object
(
[Id] => 36
[Grado] => Bachelor
)
[13] => stdClass Object
(
[Id] => 37
[Grado] => Bachelor
)
[14] => stdClass Object
(
[Id] => 42
[Grado] => PhD
)
[15] => stdClass Object
(
[Id] => 42
[Grado] => Master
)
[16] => stdClass Object
(
[Id] => 42
[Grado] => Bachelor
)
[17] => stdClass Object
(
[Id] => 50
[Grado] => Bachelor
)
[18] => stdClass Object
(
[Id] => 52
[Grado] => Bachelor
)
[19] => stdClass Object
(
[Id] => 53
[Grado] => Master
)
[20] => stdClass Object
(
[Id] => 53
[Grado] => Bachelor
)
[21] => stdClass Object
(
[Id] => 54
[Grado] => Master
)
[22] => stdClass Object
(
[Id] => 54
[Grado] => Bachelor
)
[23] => stdClass Object
(
[Id] => 55
[Grado] => Master
)
[24] => stdClass Object
(
[Id] => 55
[Grado] => Bachelor
)
[25] => stdClass Object
(
[Id] => 57
[Grado] => Bachelor
)
[26] => stdClass Object
(
[Id] => 68
[Grado] => Master
)
[27] => stdClass Object
(
[Id] => 68
[Grado] => Bachelor
)
[28] => stdClass Object
(
[Id] => 72
[Grado] => Master
)
[29] => stdClass Object
(
[Id] => 72
[Grado] => Bachelor
)
[30] => stdClass Object
(
[Id] => 77
[Grado] => Bachelor
)
[31] => stdClass Object
(
[Id] => 82
[Grado] => Bachelor
)
[32] => stdClass Object
(
[Id] => 85
[Grado] => PhD
)
[33] => stdClass Object
(
[Id] => 85
[Grado] => Master
)
[34] => stdClass Object
(
[Id] => 85
[Grado] => Bachelor
)
[35] => stdClass Object
(
[Id] => 92
[Grado] => Master
)
[36] => stdClass Object
(
[Id] => 92
[Grado] => Bachelor
)
[37] => stdClass Object
(
[Id] => 100
[Grado] => Master
)
[38] => stdClass Object
(
[Id] => 100
[Grado] => Bachelor
)
[39] => stdClass Object
(
[Id] => 111
[Grado] => Bachelor
)
[40] => stdClass Object
(
[Id] => 117
[Grado] => Master
)
[41] => stdClass Object
(
[Id] => 117
[Grado] => Bachelor
)
[42] => stdClass Object
(
[Id] => 123
[Grado] => Master
)
[43] => stdClass Object
(
[Id] => 123
[Grado] => Bachelor
)
)
)
Since, I just want to get the highest degrees (a user Id may have several historical degrees), I apply the unique() method, that according to the docs, it leaves only the first original value, in my case, I have already ordered them by hierarchy desc:
$technicalshighestdegrees = $technicalshighestdegrees->unique('Id');
And now I get the following collection (reduced to 25 items, which is exactly the total number):
Illuminate\Support\Collection Object
(
[items:protected] => Array
(
[0] => stdClass Object
(
[Id] => 3
[Degree] => Master
)
[2] => stdClass Object
(
[Id] => 4
[Degree] => Master
)
[4] => stdClass Object
(
[Id] => 6
[Degree] => Master
)
[6] => stdClass Object
(
[Id] => 18
[Degree] => Bachelor
)
[7] => stdClass Object
(
[Id] => 27
[Degree] => Bachelor
)
[8] => stdClass Object
(
[Id] => 34
[Degree] => Master
)
[10] => stdClass Object
(
[Id] => 36
[Degree] => PhD
)
[13] => stdClass Object
(
[Id] => 37
[Degree] => Bachelor
)
[14] => stdClass Object
(
[Id] => 42
[Degree] => PhD
)
[17] => stdClass Object
(
[Id] => 50
[Degree] => Bachelor
)
[18] => stdClass Object
(
[Id] => 52
[Degree] => Bachelor
)
[19] => stdClass Object
(
[Id] => 53
[Degree] => Master
)
[21] => stdClass Object
(
[Id] => 54
[Degree] => Master
)
[23] => stdClass Object
(
[Id] => 55
[Degree] => Master
)
[25] => stdClass Object
(
[Id] => 57
[Degree] => Bachelor
)
[26] => stdClass Object
(
[Id] => 68
[Degree] => Master
)
[28] => stdClass Object
(
[Id] => 72
[Degree] => Master
)
[30] => stdClass Object
(
[Id] => 77
[Degree] => Bachelor
)
[31] => stdClass Object
(
[Id] => 82
[Degree] => Bachelor
)
[32] => stdClass Object
(
[Id] => 85
[Degree] => PhD
)
[35] => stdClass Object
(
[Id] => 92
[Degree] => Master
)
[37] => stdClass Object
(
[Id] => 100
[Degree] => Master
)
[39] => stdClass Object
(
[Id] => 111
[Degree] => Bachelor
)
[40] => stdClass Object
(
[Id] => 117
[Degree] => Master
)
[42] => stdClass Object
(
[Id] => 123
[Degree] => Master
)
)
)
What I want is to get the quantities for each distinct degree value as follows:
Array
(
[Master] => 13
[Bachelor] => 9
[PhD] => 3
)
But in an Eloquent collection...
How can I achieve this?
Update for 2021/L6+
thanks #JeremyWadhams
$degrees->groupBy('Degree')->map(fn ($people) => $people->count());
// or using HigherOrder proxy on the collection
$degrees->groupBy('Degree')->map->count();
Using collection methods this would be it:
$degrees->groupBy('Degree')->map(function ($people) {
return $people->count();
});
// Collection {
// 'Master' => 13,
// 'Bachelor' => 9,
// 'PhD' => 3
// }
There is a Collection Helper called CountBy, does exactly what you need.
Collections CountBy
$degrees->countBy('Degree');
It will retourn as expected
Array
(
[Master] => 13
[Bachelor] => 9
[PhD] => 3
)
Simple :D
Could also do the following:
$master = $degrees->sum('Master');
return $master;
$bachelor = $degrees->sum('Bachelor');
return $bachelor;
$phD = $degrees->sum('PhD');
return $phD;