merge() is replacing elements from the first collection - php

I want to keep ALL elements of each collection. How do I do this?
I've tried:
$fields = Field::all();
$custom_fields = CustomField::all();
$merged_fields = $fields->merge($custom_fields);
But this code appears to be replacing the first element of $fields as $custom_fields only has 1 value right now.
Here is the dump of the collections:
Illuminate\Database\Eloquent\Collection Object
(
[items:protected] => Array
(
[0] => App\Field Object
(
[fillable:protected] => Array
(
[0] => label
[1] => name
[2] => input_type
[3] => input_options
[4] => show_in_tables
[5] => sort
)
[hidden:protected] => Array
(
[0] => pivot
)
[connection:protected] => mysql
[table:protected] =>
[primaryKey:protected] => id
[keyType:protected] => int
[incrementing] => 1
[with:protected] => Array
(
)
[withCount:protected] => Array
(
)
[perPage:protected] => 15
[exists] => 1
[wasRecentlyCreated] =>
[attributes:protected] => Array
(
[id] => 1
[label] => First Name
[name] => first_name
[input_type] => 4
[input_options] =>
[show_in_tables] => 1
[sort] => 1
[created_at] => 2017-07-17 00:20:59
[updated_at] => 2017-07-17 00:20:59
)
[original:protected] => Array
(
[id] => 1
[label] => First Name
[name] => first_name
[input_type] => 4
[input_options] =>
[show_in_tables] => 1
[sort] => 1
[created_at] => 2017-07-17 00:20:59
[updated_at] => 2017-07-17 00:20:59
)
[casts:protected] => Array
(
)
[dates:protected] => Array
(
)
[dateFormat:protected] =>
[appends:protected] => Array
(
)
[events:protected] => Array
(
)
[observables:protected] => Array
(
)
[relations:protected] => Array
(
)
[touches:protected] => Array
(
)
[timestamps] => 1
[visible:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
)
[1] => App\Field Object
(
[fillable:protected] => Array
(
[0] => label
[1] => name
[2] => input_type
[3] => input_options
[4] => show_in_tables
[5] => sort
)
[hidden:protected] => Array
(
[0] => pivot
)
[connection:protected] => mysql
[table:protected] =>
[primaryKey:protected] => id
[keyType:protected] => int
[incrementing] => 1
[with:protected] => Array
(
)
[withCount:protected] => Array
(
)
[perPage:protected] => 15
[exists] => 1
[wasRecentlyCreated] =>
[attributes:protected] => Array
(
[id] => 2
[label] => Last Name
[name] => last_name
[input_type] => 4
[input_options] =>
[show_in_tables] => 1
[sort] => 2
[created_at] => 2017-07-17 00:20:59
[updated_at] => 2017-07-17 00:20:59
)
[original:protected] => Array
(
[id] => 2
[label] => Last Name
[name] => last_name
[input_type] => 4
[input_options] =>
[show_in_tables] => 1
[sort] => 2
[created_at] => 2017-07-17 00:20:59
[updated_at] => 2017-07-17 00:20:59
)
[casts:protected] => Array
(
)
[dates:protected] => Array
(
)
[dateFormat:protected] =>
[appends:protected] => Array
(
)
[events:protected] => Array
(
)
[observables:protected] => Array
(
)
[relations:protected] => Array
(
)
[touches:protected] => Array
(
)
[timestamps] => 1
[visible:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
)
[2] => App\Field Object
(
[fillable:protected] => Array
(
[0] => label
[1] => name
[2] => input_type
[3] => input_options
[4] => show_in_tables
[5] => sort
)
[hidden:protected] => Array
(
[0] => pivot
)
[connection:protected] => mysql
[table:protected] =>
[primaryKey:protected] => id
[keyType:protected] => int
[incrementing] => 1
[with:protected] => Array
(
)
[withCount:protected] => Array
(
)
[perPage:protected] => 15
[exists] => 1
[wasRecentlyCreated] =>
[attributes:protected] => Array
(
[id] => 3
[label] => Email
[name] => email
[input_type] => 4
[input_options] =>
[show_in_tables] => 1
[sort] => 3
[created_at] => 2017-07-17 00:20:59
[updated_at] => 2017-07-17 00:20:59
)
[original:protected] => Array
(
[id] => 3
[label] => Email
[name] => email
[input_type] => 4
[input_options] =>
[show_in_tables] => 1
[sort] => 3
[created_at] => 2017-07-17 00:20:59
[updated_at] => 2017-07-17 00:20:59
)
[casts:protected] => Array
(
)
[dates:protected] => Array
(
)
[dateFormat:protected] =>
[appends:protected] => Array
(
)
[events:protected] => Array
(
)
[observables:protected] => Array
(
)
[relations:protected] => Array
(
)
[touches:protected] => Array
(
)
[timestamps] => 1
[visible:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
)
[3] => App\Field Object
(
[fillable:protected] => Array
(
[0] => label
[1] => name
[2] => input_type
[3] => input_options
[4] => show_in_tables
[5] => sort
)
[hidden:protected] => Array
(
[0] => pivot
)
[connection:protected] => mysql
[table:protected] =>
[primaryKey:protected] => id
[keyType:protected] => int
[incrementing] => 1
[with:protected] => Array
(
)
[withCount:protected] => Array
(
)
[perPage:protected] => 15
[exists] => 1
[wasRecentlyCreated] =>
[attributes:protected] => Array
(
[id] => 4
[label] => Phone
[name] => phone
[input_type] => 4
[input_options] =>
[show_in_tables] => 0
[sort] => 4
[created_at] => 2017-07-17 00:20:59
[updated_at] => 2017-07-17 00:20:59
)
[original:protected] => Array
(
[id] => 4
[label] => Phone
[name] => phone
[input_type] => 4
[input_options] =>
[show_in_tables] => 0
[sort] => 4
[created_at] => 2017-07-17 00:20:59
[updated_at] => 2017-07-17 00:20:59
)
[casts:protected] => Array
(
)
[dates:protected] => Array
(
)
[dateFormat:protected] =>
[appends:protected] => Array
(
)
[events:protected] => Array
(
)
[observables:protected] => Array
(
)
[relations:protected] => Array
(
)
[touches:protected] => Array
(
)
[timestamps] => 1
[visible:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
)
[4] => App\Field Object
(
[fillable:protected] => Array
(
[0] => label
[1] => name
[2] => input_type
[3] => input_options
[4] => show_in_tables
[5] => sort
)
[hidden:protected] => Array
(
[0] => pivot
)
[connection:protected] => mysql
[table:protected] =>
[primaryKey:protected] => id
[keyType:protected] => int
[incrementing] => 1
[with:protected] => Array
(
)
[withCount:protected] => Array
(
)
[perPage:protected] => 15
[exists] => 1
[wasRecentlyCreated] =>
[attributes:protected] => Array
(
[id] => 5
[label] => Postal Code
[name] => postal_code
[input_type] => 4
[input_options] =>
[show_in_tables] => 0
[sort] => 5
[created_at] => 2017-07-17 00:20:59
[updated_at] => 2017-07-17 00:20:59
)
[original:protected] => Array
(
[id] => 5
[label] => Postal Code
[name] => postal_code
[input_type] => 4
[input_options] =>
[show_in_tables] => 0
[sort] => 5
[created_at] => 2017-07-17 00:20:59
[updated_at] => 2017-07-17 00:20:59
)
[casts:protected] => Array
(
)
[dates:protected] => Array
(
)
[dateFormat:protected] =>
[appends:protected] => Array
(
)
[events:protected] => Array
(
)
[observables:protected] => Array
(
)
[relations:protected] => Array
(
)
[touches:protected] => Array
(
)
[timestamps] => 1
[visible:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
)
[5] => App\Field Object
(
[fillable:protected] => Array
(
[0] => label
[1] => name
[2] => input_type
[3] => input_options
[4] => show_in_tables
[5] => sort
)
[hidden:protected] => Array
(
[0] => pivot
)
[connection:protected] => mysql
[table:protected] =>
[primaryKey:protected] => id
[keyType:protected] => int
[incrementing] => 1
[with:protected] => Array
(
)
[withCount:protected] => Array
(
)
[perPage:protected] => 15
[exists] => 1
[wasRecentlyCreated] =>
[attributes:protected] => Array
(
[id] => 6
[label] => Likes Cats
[name] => likes_cats
[input_type] => 1
[input_options] => Yes
No
Maybe
[show_in_tables] => 1
[sort] => 6
[created_at] => 2017-07-17 06:21:42
[updated_at] => 2017-07-17 06:21:42
)
[original:protected] => Array
(
[id] => 6
[label] => Likes Cats
[name] => likes_cats
[input_type] => 1
[input_options] => Yes
No
Maybe
[show_in_tables] => 1
[sort] => 6
[created_at] => 2017-07-17 06:21:42
[updated_at] => 2017-07-17 06:21:42
)
[casts:protected] => Array
(
)
[dates:protected] => Array
(
)
[dateFormat:protected] =>
[appends:protected] => Array
(
)
[events:protected] => Array
(
)
[observables:protected] => Array
(
)
[relations:protected] => Array
(
)
[touches:protected] => Array
(
)
[timestamps] => 1
[visible:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
)
)
)
Illuminate\Database\Eloquent\Collection Object
(
[items:protected] => Array
(
[0] => App\CustomField Object
(
[fillable:protected] => Array
(
[0] => label
[1] => name
[2] => php_code
)
[connection:protected] => mysql
[table:protected] =>
[primaryKey:protected] => id
[keyType:protected] => int
[incrementing] => 1
[with:protected] => Array
(
)
[withCount:protected] => Array
(
)
[perPage:protected] => 15
[exists] => 1
[wasRecentlyCreated] =>
[attributes:protected] => Array
(
[id] => 1
[label] => Is Kevin
[name] => is_kevin
[php_code] => if ($lead->data('first_name') == 'Kevin') {
return 'Rules';
}
else {
return 'Drools';
}
[created_at] => 2017-07-17 05:18:27
[updated_at] => 2017-07-17 06:29:27
)
[original:protected] => Array
(
[id] => 1
[label] => Is Kevin
[name] => is_kevin
[php_code] => if ($lead->data('first_name') == 'Kevin') {
return 'Rules';
}
else {
return 'Drools';
}
[created_at] => 2017-07-17 05:18:27
[updated_at] => 2017-07-17 06:29:27
)
[casts:protected] => Array
(
)
[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] => *
)
)
)
)

Related

How do you search a deep multidimentional array in laravel?

TL;DR on bottom
I'm making a website where you add your own goals, but each of those goals can have their own goals, and they theirs etc.
Now I figured out how to get an array/eloquent collection with the goal info and all the subgoals, but I can't figure out how to best search it.
Here is a sample of the array I'm working with:
App\Goal Object
(
[guarded:protected] => Array
(
)
[connection:protected] => sqlite
[table:protected] => goals
[primaryKey:protected] => id
[keyType:protected] => int
[incrementing] => 1
[with:protected] => Array
(
)
[withCount:protected] => Array
(
)
[perPage:protected] => 15
[exists] => 1
[wasRecentlyCreated] =>
[attributes:protected] => Array
(
[id] => 2
[user_id] => 1
[goal_id] => 1
[objective] => Set up a budget
[completed] => 0
[completed_at] =>
[created_at] => 2019-11-28 16:22:57
[updated_at] => 2019-11-28 16:22:57
)
[original:protected] => Array
(
[id] => 2
[user_id] => 1
[goal_id] => 1
[objective] => Set up a budget
[completed] => 0
[completed_at] =>
[created_at] => 2019-11-28 16:22:57
[updated_at] => 2019-11-28 16:22:57
)
[changes:protected] => Array
(
)
[casts:protected] => Array
(
)
[dates:protected] => Array
(
)
[dateFormat:protected] =>
[appends:protected] => Array
(
)
[dispatchesEvents:protected] => Array
(
)
[observables:protected] => Array
(
)
[relations:protected] => Array
(
[childrenRecursive] => Illuminate\Database\Eloquent\Collection Object
(
[items:protected] => Array
(
)
)
)
[touches:protected] => Array
(
)
[timestamps] => 1
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[fillable:protected] => Array
(
)
)
1
App\Goal Object
(
[guarded:protected] => Array
(
)
[connection:protected] => sqlite
[table:protected] => goals
[primaryKey:protected] => id
[keyType:protected] => int
[incrementing] => 1
[with:protected] => Array
(
)
[withCount:protected] => Array
(
)
[perPage:protected] => 15
[exists] => 1
[wasRecentlyCreated] =>
[attributes:protected] => Array
(
[id] => 3
[user_id] => 1
[goal_id] => 1
[objective] => Save $100
[completed] => 0
[completed_at] =>
[created_at] => 2019-11-28 16:23:21
[updated_at] => 2019-11-28 16:23:21
)
[original:protected] => Array
(
[id] => 3
[user_id] => 1
[goal_id] => 1
[objective] => Save $100
[completed] => 0
[completed_at] =>
[created_at] => 2019-11-28 16:23:21
[updated_at] => 2019-11-28 16:23:21
)
[changes:protected] => Array
(
)
[casts:protected] => Array
(
)
[dates:protected] => Array
(
)
[dateFormat:protected] =>
[appends:protected] => Array
(
)
[dispatchesEvents:protected] => Array
(
)
[observables:protected] => Array
(
)
[relations:protected] => Array
(
[childrenRecursive] => Illuminate\Database\Eloquent\Collection Object
(
[items:protected] => Array
(
[0] => App\Goal Object
(
[guarded:protected] => Array
(
)
[connection:protected] => sqlite
[table:protected] => goals
[primaryKey:protected] => id
[keyType:protected] => int
[incrementing] => 1
[with:protected] => Array
(
)
[withCount:protected] => Array
(
)
[perPage:protected] => 15
[exists] => 1
[wasRecentlyCreated] =>
[attributes:protected] => Array
(
[id] => 7
[user_id] => 1
[goal_id] => 3
[objective] => Set aside $100 each payday
[completed] => 0
[completed_at] =>
[created_at] => 2019-11-28 16:25:23
[updated_at] => 2019-11-28 16:25:23
)
[original:protected] => Array
(
[id] => 7
[user_id] => 1
[goal_id] => 3
[objective] => Set aside $100 each payday
[completed] => 0
[completed_at] =>
[created_at] => 2019-11-28 16:25:23
[updated_at] => 2019-11-28 16:25:23
)
[changes:protected] => Array
(
)
[casts:protected] => Array
(
)
[dates:protected] => Array
(
)
[dateFormat:protected] =>
[appends:protected] => Array
(
)
[dispatchesEvents:protected] => Array
(
)
[observables:protected] => Array
(
)
[relations:protected] => Array
(
[goals] => Illuminate\Database\Eloquent\Collection Object
(
[items:protected] => Array
(
)
)
)
[touches:protected] => Array
(
)
[timestamps] => 1
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[fillable:protected] => Array
(
)
)
)
)
)
[touches:protected] => Array
(
)
[timestamps] => 1
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[fillable:protected] => Array
(
)
)
1
App\Goal Object
(
[guarded:protected] => Array
(
)
[connection:protected] => sqlite
[table:protected] => goals
[primaryKey:protected] => id
[keyType:protected] => int
[incrementing] => 1
[with:protected] => Array
(
)
[withCount:protected] => Array
(
)
[perPage:protected] => 15
[exists] => 1
[wasRecentlyCreated] =>
[attributes:protected] => Array
(
[id] => 4
[user_id] => 1
[goal_id] => 1
[objective] => Save $250
[completed] => 0
[completed_at] =>
[created_at] => 2019-11-28 16:23:32
[updated_at] => 2019-11-28 16:23:32
)
[original:protected] => Array
(
[id] => 4
[user_id] => 1
[goal_id] => 1
[objective] => Save $250
[completed] => 0
[completed_at] =>
[created_at] => 2019-11-28 16:23:32
[updated_at] => 2019-11-28 16:23:32
)
[changes:protected] => Array
(
)
[casts:protected] => Array
(
)
[dates:protected] => Array
(
)
[dateFormat:protected] =>
[appends:protected] => Array
(
)
[dispatchesEvents:protected] => Array
(
)
[observables:protected] => Array
(
)
[relations:protected] => Array
(
[childrenRecursive] => Illuminate\Database\Eloquent\Collection Object
(
[items:protected] => Array
(
)
)
)
[touches:protected] => Array
(
)
[timestamps] => 1
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[fillable:protected] => Array
(
)
)
1
App\Goal Object
(
[guarded:protected] => Array
(
)
[connection:protected] => sqlite
[table:protected] => goals
[primaryKey:protected] => id
[keyType:protected] => int
[incrementing] => 1
[with:protected] => Array
(
)
[withCount:protected] => Array
(
)
[perPage:protected] => 15
[exists] => 1
[wasRecentlyCreated] =>
[attributes:protected] => Array
(
[id] => 5
[user_id] => 1
[goal_id] => 1
[objective] => Save $500
[completed] => 0
[completed_at] =>
[created_at] => 2019-11-28 16:23:39
[updated_at] => 2019-11-28 16:23:39
)
[original:protected] => Array
(
[id] => 5
[user_id] => 1
[goal_id] => 1
[objective] => Save $500
[completed] => 0
[completed_at] =>
[created_at] => 2019-11-28 16:23:39
[updated_at] => 2019-11-28 16:23:39
)
[changes:protected] => Array
(
)
[casts:protected] => Array
(
)
[dates:protected] => Array
(
)
[dateFormat:protected] =>
[appends:protected] => Array
(
)
[dispatchesEvents:protected] => Array
(
)
[observables:protected] => Array
(
)
[relations:protected] => Array
(
[childrenRecursive] => Illuminate\Database\Eloquent\Collection Object
(
[items:protected] => Array
(
)
)
)
[touches:protected] => Array
(
)
[timestamps] => 1
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[fillable:protected] => Array
(
)
)
1
App\Goal Object
(
[guarded:protected] => Array
(
)
[connection:protected] => sqlite
[table:protected] => goals
[primaryKey:protected] => id
[keyType:protected] => int
[incrementing] => 1
[with:protected] => Array
(
)
[withCount:protected] => Array
(
)
[perPage:protected] => 15
[exists] => 1
[wasRecentlyCreated] =>
[attributes:protected] => Array
(
[id] => 6
[user_id] => 1
[goal_id] => 1
[objective] => Save $1000
[completed] => 0
[completed_at] =>
[created_at] => 2019-11-28 16:24:13
[updated_at] => 2019-11-28 16:24:13
)
[original:protected] => Array
(
[id] => 6
[user_id] => 1
[goal_id] => 1
[objective] => Save $1000
[completed] => 0
[completed_at] =>
[created_at] => 2019-11-28 16:24:13
[updated_at] => 2019-11-28 16:24:13
)
[changes:protected] => Array
(
)
[casts:protected] => Array
(
)
[dates:protected] => Array
(
)
[dateFormat:protected] =>
[appends:protected] => Array
(
)
[dispatchesEvents:protected] => Array
(
)
[observables:protected] => Array
(
)
[relations:protected] => Array
(
[childrenRecursive] => Illuminate\Database\Eloquent\Collection Object
(
[items:protected] => Array
(
)
)
)
[touches:protected] => Array
(
)
[timestamps] => 1
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[fillable:protected] => Array
(
)
)
1
App\Goal Object
(
[guarded:protected] => Array
(
)
[connection:protected] => sqlite
[table:protected] => goals
[primaryKey:protected] => id
[keyType:protected] => int
[incrementing] => 1
[with:protected] => Array
(
)
[withCount:protected] => Array
(
)
[perPage:protected] => 15
[exists] => 1
[wasRecentlyCreated] =>
[attributes:protected] => Array
(
[id] => 8
[user_id] => 1
[goal_id] => 1
[objective] => Save $10000
[completed] => 0
[completed_at] =>
[created_at] => 2019-11-28 16:27:24
[updated_at] => 2019-11-28 16:27:24
)
[original:protected] => Array
(
[id] => 8
[user_id] => 1
[goal_id] => 1
[objective] => Save $10000
[completed] => 0
[completed_at] =>
[created_at] => 2019-11-28 16:27:24
[updated_at] => 2019-11-28 16:27:24
)
[changes:protected] => Array
(
)
[casts:protected] => Array
(
)
[dates:protected] => Array
(
)
[dateFormat:protected] =>
[appends:protected] => Array
(
)
[dispatchesEvents:protected] => Array
(
)
[observables:protected] => Array
(
)
[relations:protected] => Array
(
[childrenRecursive] => Illuminate\Database\Eloquent\Collection Object
(
[items:protected] => Array
(
[0] => App\Goal Object
(
[guarded:protected] => Array
(
)
[connection:protected] => sqlite
[table:protected] => goals
[primaryKey:protected] => id
[keyType:protected] => int
[incrementing] => 1
[with:protected] => Array
(
)
[withCount:protected] => Array
(
)
[perPage:protected] => 15
[exists] => 1
[wasRecentlyCreated] =>
[attributes:protected] => Array
(
[id] => 9
[user_id] => 1
[goal_id] => 8
[objective] => Invest my savings
[completed] => 0
[completed_at] =>
[created_at] => 2019-11-28 16:27:50
[updated_at] => 2019-11-28 16:27:50
)
[original:protected] => Array
(
[id] => 9
[user_id] => 1
[goal_id] => 8
[objective] => Invest my savings
[completed] => 0
[completed_at] =>
[created_at] => 2019-11-28 16:27:50
[updated_at] => 2019-11-28 16:27:50
)
[changes:protected] => Array
(
)
[casts:protected] => Array
(
)
[dates:protected] => Array
(
)
[dateFormat:protected] =>
[appends:protected] => Array
(
)
[dispatchesEvents:protected] => Array
(
)
[observables:protected] => Array
(
)
[relations:protected] => Array
(
[goals] => Illuminate\Database\Eloquent\Collection Object
(
[items:protected] => Array
(
[0] => App\Goal Object
[changes:protected] => Array
(
)
[casts:protected] => Array
(
)
[dates:protected] => Array
(
)
[dateFormat:protected] =>
What I want from this array is an array of ALL the unique instances of [id] => x. It's because when you complete one goal, all subgoals should be force completed as well, but the parent goals obviously.
UPDATE
Here is how I got the array/object in the OP:
public function complete(\app\goal $goal)
{
$children = $goal->find($goal->id)->goals()->with('childrenrecursive')->get();
foreach($children as $key => $item)
{
echo "<pre>", print_r($item), "</pre>";
}
die();
}
What I need is an array with all the unique IDs of the goals from $children so when I mark a goal as completed, all children, grandchildren and so on will be marked completed as well. If I get an array of all the IDs I can run a foreach loop and mark each of them completed
UPDATE 2
Here is how my goal model is:
class Goal extends Model
{
protected $guarded = [];
public function user()
{
return $this->belongsToUser(User::class);
}
public function goals()
{
return $this->hasMany(Goal::class, 'goal_id');
}
public function childrenRecursive()
{
return $this->goals()->with('goals');
}
}
UPDATE 3
Database sample
id user_id goal_id objective completed
---------- ---------- ---------- ---------- ----------
1 1 Get rich 0
2 1 1 Set up a b 1
3 1 1 Save $100 1
4 1 1 Save $250 1
5 1 1 Save $500 1
6 1 1 Save $1000 1
7 1 3 Set aside 0
8 1 1 Save $1000 1
9 1 8 Invest my 0
10 1 9 Buy stock 0
11 1 9 Buy gold 0
12 1 9 Buy bitcoi 0
13 1 10 Buy 5x AMD 0
14 1 10 Buy 5x Int 0
15 1 8 Donations 0
16 1 15 Donate to 0
17 1 15 Send a tha 0
18 1 8 Start anot 0
19 1 16 Check out 0
20 1 19 Check if m 0
21 1 19 Find out w 0
22 1 21 Make sure 0
23 1 22 But if it 0
24 1 23 Avoid hero 0
$children is of type Collection. And Collection has many awesome functions. E.g. filter or map.
In your case it could look like following:
// Filtering results in a new collection where only elements are included that matches the condition
$filtered = $children->filter(function($item) use ($value) {
return $item->getKey() === 'x';
});
// Mapping let's you change each element in the collection. This example returns an array with only the IDs
$childrenIds = $children->map(function($item) {
return $item->getKey();
});
But if just want to update the goal and subgoals try directly updating the relation and the model itself
$goal->goals()->update(['completed' => 1]); // children
$goal->update(['completed' => 1]); // parent
Here is the full documentation: Laravel Documentation: Collection

Laravel Getting attributes data

I am new in laravel using Laravel 5.3.
I am creating a check() function in laravel model for user login
here i get all data form database useing default $this->all(); this return me a large multidymentional
array .
Illuminate\Database\Eloquent\Collection Object
(
[items:protected] => Array
(
[0] => App\wn_users Object
(
[table:protected] => wn_users
[timestamps] =>
[fillable:protected] => Array
(
[0] => role_id
[1] => firstname
[2] => lastname
[3] => username
[4] => email
[5] => password
[6] => companyname
[7] => country_id
[8] => description
[9] => ip
[10] => update_date
[11] => status
)
[connection:protected] =>
[primaryKey:protected] => id
[keyType:protected] => int
[incrementing] => 1
[with:protected] => Array
(
)
[perPage:protected] => 15
[exists] => 1
[wasRecentlyCreated] =>
[attributes:protected] => Array
(
[user_id] => 1
[role_id] => 1
[firstname] => Aman kumar
[lastname] => --
[username] => Aman kumar
[email] => aman.imaxtechnologies#gmail.com
[password] => e10adc3949ba59abbe56e057f20f883e
[companyname] => Imax
[country_id] => 123
[description] => Testing
[ip] => 192.168.1.1
[update_date] => 2017-03-20
[status] => 0
[created_at] =>
[updated_at] =>
)
[original:protected] => Array
(
[user_id] => 1
[role_id] => 1
[firstname] => Aman kumar
[lastname] => --
[username] => Aman kumar
[email] => aman.imaxtechnologies#gmail.com
[password] => e10adc3949ba59abbe56e057f20f883e
[companyname] => Imax
[country_id] => 123
[description] => Testing
[ip] => 192.168.1.1
[update_date] => 2017-03-20
[status] => 0
[created_at] =>
[updated_at] =>
)
[casts:protected] => Array
(
)
[dates:protected] => Array
(
)
[dateFormat:protected] =>
[appends:protected] => Array
(
)
[events:protected] => Array
(
)
[observables:protected] => Array
(
)
[relations:protected] => Array
(
)
[touches:protected] => Array
(
)
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
)
)
)
But i want to get only 'attributes:protected' Form whole array in laravel . I have already tried
echo $data = $this->getAttributes()['firstname']; but this returns error
Undefined index: firstname
Please help me to resolve my issue
Thanks in Advance for your help and time .
A very simple way:
$arr = $this->all()->toArray();
var_dump($arr); // oh~ array data!
So you have a collection with properties. And you can access them just like $collection->first()->firstname Or if you want to do some operation with all items use each method:
$collection = $collection->each(function ($item, $key) {
$item->firstname .= ' Smith';
});

Laralvel 5.2 Pagination does not returning from and to params

I am doing something like this
$users = CompanyModel::paginate(5);
print_r($users);exit;
when i print this , it will print "Illuminate\Pagination\LengthAwarePaginator Object" , It will return params like total , perPage, currentPage etc in it.
Now i need param like from and to which i will use for pagination at front end.
Please help!!
My response :
Illuminate\Pagination\LengthAwarePaginator Object
(
[total:protected] => 21
[lastPage:protected] => 5
[items:protected] => Illuminate\Database\Eloquent\Collection Object
(
[items:protected] => Array
(
[0] => DayJibe\DAL\Company\Models\CompanyModel Object
(
[table:protected] => companies
[fillable:protected] => Array
(
[0] => company_name
[1] => user_first_name
[2] => user_email
[3] => plan_name
[4] => total_users
[5] => monthly_payment_amount
[6] => term_ending
[7] => sign_up_date
[8] => receive_special_offers_emails
[9] => time_spent_logged_in
[10] => storage_used
)
[connection:protected] =>
[primaryKey:protected] => id
[keyType:protected] => int
[perPage:protected] => 15
[incrementing] => 1
[timestamps] => 1
[attributes:protected] => Array
(
[id] => 1
[company_name] => JohnSmithConsulting
[user_first_name] => John
[user_email] => admin#gmail.com
[plan_name] => Basic
[total_users] => 4
[monthly_payment_amount] => 10
[term_ending] => 2016-01-02 00:00:00
[sign_up_date] => 2015-01-02 00:00:00
[receive_special_offers_emails] => Y
[time_spent_logged_in] => 15
[storage_used] => 100
[created_at] => 2016-07-14 11:37:51
[updated_at] => 2016-07-14 11:37:51
)
[original:protected] => Array
(
[id] => 1
[company_name] => JohnSmithConsulting
[user_first_name] => John
[user_email] => admin#gmail.com
[plan_name] => Basic
[total_users] => 4
[monthly_payment_amount] => 10
[term_ending] => 2016-01-02 00:00:00
[sign_up_date] => 2015-01-02 00:00:00
[receive_special_offers_emails] => Y
[time_spent_logged_in] => 15
[storage_used] => 100
[created_at] => 2016-07-14 11:37:51
[updated_at] => 2016-07-14 11:37:51
)
[relations:protected] => Array
(
)
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[appends:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
[dates:protected] => Array
(
)
[dateFormat:protected] =>
[casts:protected] => Array
(
)
[touches:protected] => Array
(
)
[observables:protected] => Array
(
)
[with:protected] => Array
(
)
[morphClass:protected] =>
[exists] => 1
[wasRecentlyCreated] =>
)
[1] => DayJibe\DAL\Company\Models\CompanyModel Object
(
[table:protected] => companies
[fillable:protected] => Array
(
[0] => company_name
[1] => user_first_name
[2] => user_email
[3] => plan_name
[4] => total_users
[5] => monthly_payment_amount
[6] => term_ending
[7] => sign_up_date
[8] => receive_special_offers_emails
[9] => time_spent_logged_in
[10] => storage_used
)
[connection:protected] =>
[primaryKey:protected] => id
[keyType:protected] => int
[perPage:protected] => 15
[incrementing] => 1
[timestamps] => 1
[attributes:protected] => Array
(
[id] => 2
[company_name] => James Engineering
[user_first_name] => James
[user_email] => James#gmail.com
[plan_name] => Enterprise
[total_users] => 50
[monthly_payment_amount] => 250
[term_ending] => 2016-04-05 00:00:00
[sign_up_date] => 2016-01-02 00:00:00
[receive_special_offers_emails] => N
[time_spent_logged_in] => 25
[storage_used] => 250
[created_at] => 2016-07-15 06:33:34
[updated_at] => 2016-07-15 06:33:34
)
[original:protected] => Array
(
[id] => 2
[company_name] => James Engineering
[user_first_name] => James
[user_email] => James#gmail.com
[plan_name] => Enterprise
[total_users] => 50
[monthly_payment_amount] => 250
[term_ending] => 2016-04-05 00:00:00
[sign_up_date] => 2016-01-02 00:00:00
[receive_special_offers_emails] => N
[time_spent_logged_in] => 25
[storage_used] => 250
[created_at] => 2016-07-15 06:33:34
[updated_at] => 2016-07-15 06:33:34
)
[relations:protected] => Array
(
)
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[appends:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
[dates:protected] => Array
(
)
[dateFormat:protected] =>
[casts:protected] => Array
(
)
[touches:protected] => Array
(
)
[observables:protected] => Array
(
)
[with:protected] => Array
(
)
[morphClass:protected] =>
[exists] => 1
[wasRecentlyCreated] =>
)
[2] => DayJibe\DAL\Company\Models\CompanyModel Object
(
[table:protected] => companies
[fillable:protected] => Array
(
[0] => company_name
[1] => user_first_name
[2] => user_email
[3] => plan_name
[4] => total_users
[5] => monthly_payment_amount
[6] => term_ending
[7] => sign_up_date
[8] => receive_special_offers_emails
[9] => time_spent_logged_in
[10] => storage_used
)
[connection:protected] =>
[primaryKey:protected] => id
[keyType:protected] => int
[perPage:protected] => 15
[incrementing] => 1
[timestamps] => 1
[attributes:protected] => Array
(
[id] => 3
[company_name] => Joe Engineering LTD.
[user_first_name] => Joe
[user_email] => Joe#gmail.com
[plan_name] => Freelance
[total_users] => 36
[monthly_payment_amount] => 500
[term_ending] => 2016-08-08 00:00:00
[sign_up_date] => 2015-08-08 00:00:00
[receive_special_offers_emails] => Y
[time_spent_logged_in] => 50
[storage_used] => 500
[created_at] => 2016-07-15 06:35:39
[updated_at] => 2016-07-15 06:35:39
)
[original:protected] => Array
(
[id] => 3
[company_name] => Joe Engineering LTD.
[user_first_name] => Joe
[user_email] => Joe#gmail.com
[plan_name] => Freelance
[total_users] => 36
[monthly_payment_amount] => 500
[term_ending] => 2016-08-08 00:00:00
[sign_up_date] => 2015-08-08 00:00:00
[receive_special_offers_emails] => Y
[time_spent_logged_in] => 50
[storage_used] => 500
[created_at] => 2016-07-15 06:35:39
[updated_at] => 2016-07-15 06:35:39
)
[relations:protected] => Array
(
)
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[appends:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
[dates:protected] => Array
(
)
[dateFormat:protected] =>
[casts:protected] => Array
(
)
[touches:protected] => Array
(
)
[observables:protected] => Array
(
)
[with:protected] => Array
(
)
[morphClass:protected] =>
[exists] => 1
[wasRecentlyCreated] =>
)
[3] => DayJibe\DAL\Company\Models\CompanyModel Object
(
[table:protected] => companies
[fillable:protected] => Array
(
[0] => company_name
[1] => user_first_name
[2] => user_email
[3] => plan_name
[4] => total_users
[5] => monthly_payment_amount
[6] => term_ending
[7] => sign_up_date
[8] => receive_special_offers_emails
[9] => time_spent_logged_in
[10] => storage_used
)
[connection:protected] =>
[primaryKey:protected] => id
[keyType:protected] => int
[perPage:protected] => 15
[incrementing] => 1
[timestamps] => 1
[attributes:protected] => Array
(
[id] => 4
[company_name] => Michal Engineering LTD.
[user_first_name] => Michal
[user_email] => Michal#gmail.com
[plan_name] => Basic
[total_users] => 45
[monthly_payment_amount] => 400
[term_ending] => 2016-09-10 00:00:00
[sign_up_date] => 2016-01-05 00:00:00
[receive_special_offers_emails] => N
[time_spent_logged_in] => 40
[storage_used] => 150
[created_at] => 2016-07-15 06:36:57
[updated_at] => 2016-07-15 06:36:57
)
[original:protected] => Array
(
[id] => 4
[company_name] => Michal Engineering LTD.
[user_first_name] => Michal
[user_email] => Michal#gmail.com
[plan_name] => Basic
[total_users] => 45
[monthly_payment_amount] => 400
[term_ending] => 2016-09-10 00:00:00
[sign_up_date] => 2016-01-05 00:00:00
[receive_special_offers_emails] => N
[time_spent_logged_in] => 40
[storage_used] => 150
[created_at] => 2016-07-15 06:36:57
[updated_at] => 2016-07-15 06:36:57
)
[relations:protected] => Array
(
)
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[appends:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
[dates:protected] => Array
(
)
[dateFormat:protected] =>
[casts:protected] => Array
(
)
[touches:protected] => Array
(
)
[observables:protected] => Array
(
)
[with:protected] => Array
(
)
[morphClass:protected] =>
[exists] => 1
[wasRecentlyCreated] =>
)
[4] => DayJibe\DAL\Company\Models\CompanyModel Object
(
[table:protected] => companies
[fillable:protected] => Array
(
[0] => company_name
[1] => user_first_name
[2] => user_email
[3] => plan_name
[4] => total_users
[5] => monthly_payment_amount
[6] => term_ending
[7] => sign_up_date
[8] => receive_special_offers_emails
[9] => time_spent_logged_in
[10] => storage_used
)
[connection:protected] =>
[primaryKey:protected] => id
[keyType:protected] => int
[perPage:protected] => 15
[incrementing] => 1
[timestamps] => 1
[attributes:protected] => Array
(
[id] => 5
[company_name] => Michal Engineering LTD.
[user_first_name] => Michal
[user_email] => Michal#gmail.com
[plan_name] => Basic
[total_users] => 45
[monthly_payment_amount] => 400
[term_ending] => 0000-00-00 00:00:00
[sign_up_date] => 0000-00-00 00:00:00
[receive_special_offers_emails] => N
[time_spent_logged_in] => 40
[storage_used] => 150
[created_at] => 2016-07-15 07:58:36
[updated_at] => 2016-07-15 07:58:36
)
[original:protected] => Array
(
[id] => 5
[company_name] => Michal Engineering LTD.
[user_first_name] => Michal
[user_email] => Michal#gmail.com
[plan_name] => Basic
[total_users] => 45
[monthly_payment_amount] => 400
[term_ending] => 0000-00-00 00:00:00
[sign_up_date] => 0000-00-00 00:00:00
[receive_special_offers_emails] => N
[time_spent_logged_in] => 40
[storage_used] => 150
[created_at] => 2016-07-15 07:58:36
[updated_at] => 2016-07-15 07:58:36
)
[relations:protected] => Array
(
)
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[appends:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
[dates:protected] => Array
(
)
[dateFormat:protected] =>
[casts:protected] => Array
(
)
[touches:protected] => Array
(
)
[observables:protected] => Array
(
)
[with:protected] => Array
(
)
[morphClass:protected] =>
[exists] => 1
[wasRecentlyCreated] =>
)
)
)
[perPage:protected] => 5
[currentPage:protected] => 1
[path:protected] => http://dayjibe.localhost.com/api/v1.0/company
[query:protected] => Array
(
)
[fragment:protected] =>
[pageName:protected] => page
)
I found this :
$users = CompanyModel::paginate(5);
Response::json($users);
If we are doing this, then in JSON response it will automatically add the parameters like per_page,current_page,from,to etc.
So yes we need to do Response::json and then get required Paramus from it.
Thanks guys :)

How to keep order of collected database row the same as array passed to where clause

I am working on a project using Laravel 4.2 and I have a line that retrieves some rows from a table called Gifts
$theGifts = Gift::whereIn('id', $giftImages)->get();
This variable $giftImages is an array and values are ordered like this
Array
(
[0] => 5
[1] => 2
[2] => 3
[3] => 4
)
When I output this one however $theGifts the retrieved results are not in the same order as the array of ids is and I need it to be in the same order..
How can I insure that the order is the same? I am using the data from the object in the controller, not the view by the way.
The printout of the Gift::all() looks like this (in case someone needs to compare)..
Illuminate\Database\Eloquent\Collection Object
(
[items:protected] => Array
(
[0] => Gift Object
(
[table:protected] => gifts
[connection:protected] =>
[primaryKey:protected] => id
[perPage:protected] => 15
[incrementing] => 1
[timestamps] => 1
[attributes:protected] => Array
(
[id] => 2
[name] => Banana
[image] => bananas-icon.png
[value] => 10
)
[original:protected] => Array
(
[id] => 2
[name] => Banana
[image] => bananas-icon.png
[value] => 10
)
[relations:protected] => Array
(
)
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[appends:protected] => Array
(
)
[fillable:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
[dates:protected] => Array
(
)
[touches:protected] => Array
(
)
[observables:protected] => Array
(
)
[with:protected] => Array
(
)
[morphClass:protected] =>
[exists] => 1
)
[1] => Gift Object
(
[table:protected] => gifts
[connection:protected] =>
[primaryKey:protected] => id
[perPage:protected] => 15
[incrementing] => 1
[timestamps] => 1
[attributes:protected] => Array
(
[id] => 3
[name] => Cherry Cake
[image] => Cherry-Cake-icon.png
[value] => 15
)
[original:protected] => Array
(
[id] => 3
[name] => Cherry Cake
[image] => Cherry-Cake-icon.png
[value] => 15
)
[relations:protected] => Array
(
)
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[appends:protected] => Array
(
)
[fillable:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
[dates:protected] => Array
(
)
[touches:protected] => Array
(
)
[observables:protected] => Array
(
)
[with:protected] => Array
(
)
[morphClass:protected] =>
[exists] => 1
)
[2] => Gift Object
(
[table:protected] => gifts
[connection:protected] =>
[primaryKey:protected] => id
[perPage:protected] => 15
[incrementing] => 1
[timestamps] => 1
[attributes:protected] => Array
(
[id] => 4
[name] => Coconut
[image] => coconut-icon.png
[value] => 5
)
[original:protected] => Array
(
[id] => 4
[name] => Coconut
[image] => coconut-icon.png
[value] => 5
)
[relations:protected] => Array
(
)
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[appends:protected] => Array
(
)
[fillable:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
[dates:protected] => Array
(
)
[touches:protected] => Array
(
)
[observables:protected] => Array
(
)
[with:protected] => Array
(
)
[morphClass:protected] =>
[exists] => 1
)
[3] => Gift Object
(
[table:protected] => gifts
[connection:protected] =>
[primaryKey:protected] => id
[perPage:protected] => 15
[incrementing] => 1
[timestamps] => 1
[attributes:protected] => Array
(
[id] => 5
[name] => Lemon
[image] => lemon-icon.png
[value] => 3
)
[original:protected] => Array
(
[id] => 5
[name] => Lemon
[image] => lemon-icon.png
[value] => 3
)
[relations:protected] => Array
(
)
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[appends:protected] => Array
(
)
[fillable:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
[dates:protected] => Array
(
)
[touches:protected] => Array
(
)
[observables:protected] => Array
(
)
[with:protected] => Array
(
)
[morphClass:protected] =>
[exists] => 1
)
)
)
I believe this will do it for you:
Gift::whereIn('id', $giftImages)
->orderBy(DB::raw('FIELD(id, ' . implode(',', $giftImages) . ')'), 'ASC')
->get();
Taken from this question. You have to use DB::raw() since Laravel doesn't provide a native way to do this. Untested...

Laravel sort group array in collection

I get a collection after database query. And then I group it by passing a closure callback. And then last, I want to sort these grouped array in collection using each which pass a closure callback to sort array. But NOT working. Maybe it's protected , how to achieve this?
My code at PostController
$post->comments = $post->comments->groupBy(function($comment){
return $comment->parent_id == null ? "-1": $comment->parent_id;
})->each(function($comment){
// TODO: sort array
uasort($comment, function($a, $b){
return $a->created_at->lte($b->created_at)? 1: -1;
});
return $comment;
});
Debugbar::info($post->comments);
Info of $post->comments
Illuminate\Database\Eloquent\Collection Object
(
[items:protected] => Array
(
[-1] => Array
(
[0] => Comment Object
(
[table:protected] => comments
[fillable:protected] => Array
(
[0] => id
)
[connection:protected] =>
[primaryKey:protected] => id
[perPage:protected] => 15
[incrementing] => 1
[timestamps] => 1
[attributes:protected] => Array
(
[id] => 1
[user_id] => 4
[post_id] => 1
[parent_id] =>
[comment] => Eligendi ipsum saepe qui velit laudantium.
[created_at] => 2013-11-06 13:28:49
[updated_at] => 2013-11-06 13:28:49
)
[original:protected] => Array
(
[id] => 1
[user_id] => 4
[post_id] => 1
[parent_id] =>
[comment] => Eligendi ipsum saepe qui velit laudantium.
[created_at] => 2013-11-06 13:28:49
[updated_at] => 2013-11-06 13:28:49
)
[relations:protected] => Array
(
)
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[appends:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
[dates:protected] => Array
(
)
[touches:protected] => Array
(
)
[observables:protected] => Array
(
)
[with:protected] => Array
(
)
[exists] => 1
[softDelete:protected] =>
)
[1] => Comment Object
(
[table:protected] => comments
[fillable:protected] => Array
(
[0] => id
)
[connection:protected] =>
[primaryKey:protected] => id
[perPage:protected] => 15
[incrementing] => 1
[timestamps] => 1
[attributes:protected] => Array
(
[id] => 2
[user_id] => 63
[post_id] => 1
[parent_id] =>
[comment] => Aperiam ut molestiae.
[created_at] => 2013-08-18 00:15:24
[updated_at] => 2013-08-18 00:15:24
)
[original:protected] => Array
(
[id] => 2
[user_id] => 63
[post_id] => 1
[parent_id] =>
[comment] => Aperiam ut molestiae.
[created_at] => 2013-08-18 00:15:24
[updated_at] => 2013-08-18 00:15:24
)
[relations:protected] => Array
(
)
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[appends:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
[dates:protected] => Array
(
)
[touches:protected] => Array
(
)
[observables:protected] => Array
(
)
[with:protected] => Array
(
)
[exists] => 1
[softDelete:protected] =>
)
[2] => Comment Object
(
[table:protected] => comments
[fillable:protected] => Array
(
[0] => id
)
[connection:protected] =>
[primaryKey:protected] => id
[perPage:protected] => 15
[incrementing] => 1
[timestamps] => 1
[attributes:protected] => Array
(
[id] => 3
[user_id] => 4
[post_id] => 1
[parent_id] =>
[comment] => Doloribus ipsa velit unde veritatis qui.
[created_at] => 2013-09-20 05:19:45
[updated_at] => 2013-09-20 05:19:45
)
[original:protected] => Array
(
[id] => 3
[user_id] => 4
[post_id] => 1
[parent_id] =>
[comment] => Doloribus ipsa velit unde veritatis qui.
[created_at] => 2013-09-20 05:19:45
[updated_at] => 2013-09-20 05:19:45
)
[relations:protected] => Array
(
)
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[appends:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
[dates:protected] => Array
(
)
[touches:protected] => Array
(
)
[observables:protected] => Array
(
)
[with:protected] => Array
(
)
[exists] => 1
[softDelete:protected] =>
)
[19] => Comment Object
(
[table:protected] => comments
[fillable:protected] => Array
(
[0] => id
)
[connection:protected] =>
[primaryKey:protected] => id
[perPage:protected] => 15
[incrementing] => 1
[timestamps] => 1
[attributes:protected] => Array
(
[id] => 27
[user_id] => 100
[post_id] => 1
[parent_id] =>
[comment] => dsdfsdfdg
[created_at] => 2014-02-17 09:06:08
[updated_at] => 2014-02-17 09:06:08
)
[original:protected] => Array
(
[id] => 27
[user_id] => 100
[post_id] => 1
[parent_id] =>
[comment] => dsdfsdfdg
[created_at] => 2014-02-17 09:06:08
[updated_at] => 2014-02-17 09:06:08
)
[relations:protected] => Array
(
)
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[appends:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
[dates:protected] => Array
(
)
[touches:protected] => Array
(
)
[observables:protected] => Array
(
)
[with:protected] => Array
(
)
[exists] => 1
[softDelete:protected] =>
)
[20] => Comment Object
(
[table:protected] => comments
[fillable:protected] => Array
(
[0] => id
)
[connection:protected] =>
[primaryKey:protected] => id
[perPage:protected] => 15
[incrementing] => 1
[timestamps] => 1
[attributes:protected] => Array
(
[id] => 28
[user_id] => 100
[post_id] => 1
[parent_id] =>
[comment] => dsdfsdfdg
[created_at] => 2014-02-17 09:08:14
[updated_at] => 2014-02-17 09:08:14
)
[original:protected] => Array
(
[id] => 28
[user_id] => 100
[post_id] => 1
[parent_id] =>
[comment] => dsdfsdfdg
[created_at] => 2014-02-17 09:08:14
[updated_at] => 2014-02-17 09:08:14
)
[relations:protected] => Array
(
)
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[appends:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
[dates:protected] => Array
(
)
[touches:protected] => Array
(
)
[observables:protected] => Array
(
)
[with:protected] => Array
(
)
[exists] => 1
[softDelete:protected] =>
)
)
[5] => Array
(
[0] => Comment Object
(
[table:protected] => comments
[fillable:protected] => Array
(
[0] => id
)
[connection:protected] =>
[primaryKey:protected] => id
[perPage:protected] => 15
[incrementing] => 1
[timestamps] => 1
[attributes:protected] => Array
(
[id] => 7
[user_id] => 12
[post_id] => 1
[parent_id] => 5
[comment] => Consequatur aut consequuntur ut voluptatem et.
[created_at] => 2013-09-19 11:21:35
[updated_at] => 2013-09-19 11:21:35
)
[original:protected] => Array
(
[id] => 7
[user_id] => 12
[post_id] => 1
[parent_id] => 5
[comment] => Consequatur aut consequuntur ut voluptatem et.
[created_at] => 2013-09-19 11:21:35
[updated_at] => 2013-09-19 11:21:35
)
[relations:protected] => Array
(
)
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[appends:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
[dates:protected] => Array
(
)
[touches:protected] => Array
(
)
[observables:protected] => Array
(
)
[with:protected] => Array
(
)
[exists] => 1
[softDelete:protected] =>
)
)
)
[2] => Array
(
[0] => Comment Object
(
[table:protected] => comments
[fillable:protected] => Array
(
[0] => id
)
[connection:protected] =>
[primaryKey:protected] => id
[perPage:protected] => 15
[incrementing] => 1
[timestamps] => 1
[attributes:protected] => Array
(
[id] => 9
[user_id] => 74
[post_id] => 1
[parent_id] => 2
[comment] => Omnis est tenetur eum quo totam.
[created_at] => 2013-11-12 12:40:41
[updated_at] => 2013-11-12 12:40:41
)
[original:protected] => Array
(
[id] => 9
[user_id] => 74
[post_id] => 1
[parent_id] => 2
[comment] => Omnis est tenetur eum quo totam.
[created_at] => 2013-11-12 12:40:41
[updated_at] => 2013-11-12 12:40:41
)
[relations:protected] => Array
(
)
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[appends:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
[dates:protected] => Array
(
)
[touches:protected] => Array
(
)
[observables:protected] => Array
(
)
[with:protected] => Array
(
)
[exists] => 1
[softDelete:protected] =>
)
)
[1] => Array
(
[0] => Comment Object
(
[table:protected] => comments
[fillable:protected] => Array
(
[0] => id
)
[connection:protected] =>
[primaryKey:protected] => id
[perPage:protected] => 15
[incrementing] => 1
[timestamps] => 1
[attributes:protected] => Array
(
[id] => 10
[user_id] => 66
[post_id] => 1
[parent_id] => 1
[comment] => Non consequatur excepturi molestias tempore voluptatem.
[created_at] => 2013-09-19 09:13:02
[updated_at] => 2013-09-19 09:13:02
)
[original:protected] => Array
(
[id] => 10
[user_id] => 66
[post_id] => 1
[parent_id] => 1
[comment] => Non consequatur excepturi molestias tempore voluptatem.
[created_at] => 2013-09-19 09:13:02
[updated_at] => 2013-09-19 09:13:02
)
[relations:protected] => Array
(
)
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[appends:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
[dates:protected] => Array
(
)
[touches:protected] => Array
(
)
[observables:protected] => Array
(
)
[with:protected] => Array
(
)
[exists] => 1
[softDelete:protected] =>
)
)
)
)
You can see that group of "-1" is not order by creation date.
So far, I achieve this by create a private function in controller to process grouped comment collection object.
This is how the function like:
private function commentToArrayAndSort(Illuminate\Database\Eloquent\Collection $groupedComments, Closure $sortClosure, Closure $childrenSortClosure = null)
{
$formattedComments = array();
$keys = array_keys($groupedComments->toArray());
for($i = 0; $i < count($keys); $i++)
{
$comments = $groupedComments[$keys[$i]];
$formattedComments[$keys[$i]] = array();
$counter = 0;
foreach($comments as $comment)
{
$commentArr = $comment->toArray();
$commentObj = new Comment(array(
"id" => $commentArr['id'],
"user_id" => $commentArr['user_id'],
"post_id" => $commentArr['post_id'],
"parent_id" => $commentArr['parent_id'],
"comment" => $commentArr['comment'],
"created_at" => $commentArr['created_at'],
"updated_at" => $commentArr['updated_at'],
));
$commentObj->setRelations(array('author'=>new User($comment->toArray()['author'])));
array_push($formattedComments[$keys[$i]], $commentObj);
$counter++;
}
// Sort Array
if ($counter > 1) {
uasort($formattedComments[$keys[$i]], $sortClosure);
}
}
return $formattedComments;
}
And then call it like this:
$post->comments = $this->commentToArrayAndSort($groupedComments, function($a, $b){
return $a->created_at->gte($b->created_at) ? -1 : 1;
});
I don't know if there's a better solution for this. So far I got this.
If anyone can improve my answer or has a better solution, plz post it to share and give me a help
Thanks :)
I was searching for something related :
I have to manipulate my data from Eloquent in the controller before passing it to the view.
So for example :
$events = App\Models\Event::->get();
$eventsDecade = [];
foreach ($events as $key => $value) {
$decade = substr(date('Y', strtotime($value->date)), 0, -1)."0";
$eventsDecade[$decade][] = new App\Models\Event($value->toArray());
}
By doing this I'm creating a new array with the decade and inside each decade an Event Object that i can use as a normal object : {{ $event->name }} in a foreach for example.
Not sure your question was exactly that...

Categories