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
Related
I'm trying to display a list of names from a query in Laravel, I can't manage to make the results show properly.
Here's what I've got in my controller:
public function getSettings()
{
$contents = explode(",", File::get(storage_path('app/public/users/0/config/social_module/buddy.txt')));
foreach($contents as $key => $value){
$buddylist[$value] = User::select('fullname as name')->where('id', $value)->get();
}
return view('welcome', compact('buddylist'));
}
Here's my view looks like:
#foreach($buddylist as $buddy)
<ul>
<li>{{ $buddy }}</li>
</ul>
#endforeach
If I use $buddy=>$key it shows nothing.
And if I use this instead, this is what the results look like:
[{"name":"Alix Earnshaw"}]
[{"name":"Horacio Esgate"}]
[{"name":"Patrizia Anthony"}]
[{"name":"Ann-marie McLewd"}]
[{"name":"Boote Poulsum"}]
[{"name":"Any Bandy"}]
[{"name":"Francene Lelliott"}]
[{"name":"Ingelbert Abdy"}]
[{"name":"Nickolaus Di Biaggi"}]
[{"name":"Constantina Wreight"}]
[{"name":"Clarissa Coomer"}]
print_r($contents)
Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 12 [6] => 43 [7] => 74 [8] => 23 [9] => 67 [10] => 32 )
print_r($buddylist)
Array ( [1] => Illuminate\Database\Eloquent\Collection Object ( [items:protected] => Array ( [0] => App\User Object ( [table:protected] => users [fillable:protected] => Array ( ) [hidden:protected] => Array ( [0] => password ) [primaryKey:protected] => id [keyType:protected] => bigInteger [timestamps] => [remember_token] => [connection:protected] => mysql [incrementing] => 1 [with:protected] => Array ( ) [withCount:protected] => Array ( ) [perPage:protected] => 15 [exists] => 1 [wasRecentlyCreated] => [attributes:protected] => Array ( [name] => Alix Earnshaw ) [original:protected] => Array ( [name] => Alix Earnshaw ) [changes:protected] => Array ( ) [casts:protected] => Array ( ) [dates:protected] => Array ( ) [dateFormat:protected] => [appends:protected] => Array ( ) [dispatchesEvents:protected] => Array ( ) [observables:protected] => Array ( ) [relations:protected] => Array ( ) [touches:protected] => Array ( ) [visible:protected] => Array ( ) [guarded:protected] => Array ( [0] => * ) ) ) ) [2] => Illuminate\Database\Eloquent\Collection Object ( [items:protected] => Array ( [0] => App\User Object ( [table:protected] => users [fillable:protected] => Array ( ) [hidden:protected] => Array ( [0] => password ) [primaryKey:protected] => id [keyType:protected] => bigInteger [timestamps] => [remember_token] => [connection:protected] => mysql [incrementing] => 1 [with:protected] => Array ( ) [withCount:protected] => Array ( ) [perPage:protected] => 15 [exists] => 1 [wasRecentlyCreated] => [attributes:protected] => Array ( [name] => Horacio Esgate ) [original:protected] => Array ( [name] => Horacio Esgate ) [changes:protected] => Array ( ) [casts:protected] => Array ( ) [dates:protected] => Array ( ) [dateFormat:protected] => [appends:protected] => Array ( ) [dispatchesEvents:protected] => Array ( ) [observables:protected] => Array ( ) [relations:protected] => Array ( ) [touches:protected] => Array ( ) [visible:protected] => Array ( ) [guarded:protected] => Array ( [0] => * ) ) ) ) [3] => Illuminate\Database\Eloquent\Collection Object ( [items:protected] => Array ( [0] => App\User Object ( [table:protected] => users [fillable:protected] => Array ( ) [hidden:protected] => Array ( [0] => password ) [primaryKey:protected] => id [keyType:protected] => bigInteger [timestamps] => [remember_token] => [connection:protected] => mysql [incrementing] => 1 [with:protected] => Array ( ) [withCount:protected] => Array ( ) [perPage:protected] => 15 [exists] => 1 [wasRecentlyCreated] => [attributes:protected] => Array ( [name] => Patrizia Anthony ) [original:protected] => Array ( [name] => Patrizia Anthony ) [changes:protected] => Array ( ) [casts:protected] => Array ( ) [dates:protected] => Array ( ) [dateFormat:protected] => [appends:protected] => Array ( ) [dispatchesEvents:protected] => Array ( ) [observables:protected] => Array ( ) [relations:protected] => Array ( ) [touches:protected] => Array ( ) [visible:protected] => Array ( ) [guarded:protected] => Array ( [0] => * ) ) ) ) [4] => Illuminate\Database\Eloquent\Collection Object ( [items:protected] => Array ( [0] => App\User Object ( [table:protected] => users [fillable:protected] => Array ( ) [hidden:protected] => Array ( [0] => password ) [primaryKey:protected] => id [keyType:protected] => bigInteger [timestamps] => [remember_token] => [connection:protected] => mysql [incrementing] => 1 [with:protected] => Array ( ) [withCount:protected] => Array ( ) [perPage:protected] => 15 [exists] => 1 [wasRecentlyCreated] => [attributes:protected] => Array ( [name] => Ann-marie McLewd ) [original:protected] => Array ( [name] => Ann-marie McLewd ) [changes:protected] => Array ( ) [casts:protected] => Array ( ) [dates:protected] => Array ( ) [dateFormat:protected] => [appends:protected] => Array ( ) [dispatchesEvents:protected] => Array ( ) [observables:protected] => Array ( ) [relations:protected] => Array ( ) [touches:protected] => Array ( ) [visible:protected] => Array ( ) [guarded:protected] => Array ( [0] => * ) ) ) ) [5] => Illuminate\Database\Eloquent\Collection Object ( [items:protected] => Array ( [0] => App\User Object ( [table:protected] => users [fillable:protected] => Array ( ) [hidden:protected] => Array ( [0] => password ) [primaryKey:protected] => id [keyType:protected] => bigInteger [timestamps] => [remember_token] => [connection:protected] => mysql [incrementing] => 1 [with:protected] => Array ( ) [withCount:protected] => Array ( ) [perPage:protected] => 15 [exists] => 1 [wasRecentlyCreated] => [attributes:protected] => Array ( [name] => Boote Poulsum ) [original:protected] => Array ( [name] => Boote Poulsum ) [changes:protected] => Array ( ) [casts:protected] => Array ( ) [dates:protected] => Array ( ) [dateFormat:protected] => [appends:protected] => Array ( ) [dispatchesEvents:protected] => Array ( ) [observables:protected] => Array ( ) [relations:protected] => Array ( ) [touches:protected] => Array ( ) [visible:protected] => Array ( ) [guarded:protected] => Array ( [0] => * ) ) ) ) [12] => Illuminate\Database\Eloquent\Collection Object ( [items:protected] => Array ( [0] => App\User Object ( [table:protected] => users [fillable:protected] => Array ( ) [hidden:protected] => Array ( [0] => password ) [primaryKey:protected] => id [keyType:protected] => bigInteger [timestamps] => [remember_token] => [connection:protected] => mysql [incrementing] => 1 [with:protected] => Array ( ) [withCount:protected] => Array ( ) [perPage:protected] => 15 [exists] => 1 [wasRecentlyCreated] => [attributes:protected] => Array ( [name] => Any Bandy ) [original:protected] => Array ( [name] => Any Bandy ) [changes:protected] => Array ( ) [casts:protected] => Array ( ) [dates:protected] => Array ( ) [dateFormat:protected] => [appends:protected] => Array ( ) [dispatchesEvents:protected] => Array ( ) [observables:protected] => Array ( ) [relations:protected] => Array ( ) [touches:protected] => Array ( ) [visible:protected] => Array ( ) [guarded:protected] => Array ( [0] => * ) ) ) ) [43] => Illuminate\Database\Eloquent\Collection Object ( [items:protected] => Array ( [0] => App\User Object ( [table:protected] => users [fillable:protected] => Array ( ) [hidden:protected] => Array ( [0] => password ) [primaryKey:protected] => id [keyType:protected] => bigInteger [timestamps] => [remember_token] => [connection:protected] => mysql [incrementing] => 1 [with:protected] => Array ( ) [withCount:protected] => Array ( ) [perPage:protected] => 15 [exists] => 1 [wasRecentlyCreated] => [attributes:protected] => Array ( [name] => Francene Lelliott ) [original:protected] => Array ( [name] => Francene Lelliott ) [changes:protected] => Array ( ) [casts:protected] => Array ( ) [dates:protected] => Array ( ) [dateFormat:protected] => [appends:protected] => Array ( ) [dispatchesEvents:protected] => Array ( ) [observables:protected] => Array ( ) [relations:protected] => Array ( ) [touches:protected] => Array ( ) [visible:protected] => Array ( ) [guarded:protected] => Array ( [0] => * ) ) ) ) [74] => Illuminate\Database\Eloquent\Collection Object ( [items:protected] => Array ( [0] => App\User Object ( [table:protected] => users [fillable:protected] => Array ( ) [hidden:protected] => Array ( [0] => password ) [primaryKey:protected] => id [keyType:protected] => bigInteger [timestamps] => [remember_token] => [connection:protected] => mysql [incrementing] => 1 [with:protected] => Array ( ) [withCount:protected] => Array ( ) [perPage:protected] => 15 [exists] => 1 [wasRecentlyCreated] => [attributes:protected] => Array ( [name] => Ingelbert Abdy ) [original:protected] => Array ( [name] => Ingelbert Abdy ) [changes:protected] => Array ( ) [casts:protected] => Array ( ) [dates:protected] => Array ( ) [dateFormat:protected] => [appends:protected] => Array ( ) [dispatchesEvents:protected] => Array ( ) [observables:protected] => Array ( ) [relations:protected] => Array ( ) [touches:protected] => Array ( ) [visible:protected] => Array ( ) [guarded:protected] => Array ( [0] => * ) ) ) ) [23] => Illuminate\Database\Eloquent\Collection Object ( [items:protected] => Array ( [0] => App\User Object ( [table:protected] => users [fillable:protected] => Array ( ) [hidden:protected] => Array ( [0] => password ) [primaryKey:protected] => id [keyType:protected] => bigInteger [timestamps] => [remember_token] => [connection:protected] => mysql [incrementing] => 1 [with:protected] => Array ( ) [withCount:protected] => Array ( ) [perPage:protected] => 15 [exists] => 1 [wasRecentlyCreated] => [attributes:protected] => Array ( [name] => Nickolaus Di Biaggi ) [original:protected] => Array ( [name] => Nickolaus Di Biaggi ) [changes:protected] => Array ( ) [casts:protected] => Array ( ) [dates:protected] => Array ( ) [dateFormat:protected] => [appends:protected] => Array ( ) [dispatchesEvents:protected] => Array ( ) [observables:protected] => Array ( ) [relations:protected] => Array ( ) [touches:protected] => Array ( ) [visible:protected] => Array ( ) [guarded:protected] => Array ( [0] => * ) ) ) ) [67] => Illuminate\Database\Eloquent\Collection Object ( [items:protected] => Array ( [0] => App\User Object ( [table:protected] => users [fillable:protected] => Array ( ) [hidden:protected] => Array ( [0] => password ) [primaryKey:protected] => id [keyType:protected] => bigInteger [timestamps] => [remember_token] => [connection:protected] => mysql [incrementing] => 1 [with:protected] => Array ( ) [withCount:protected] => Array ( ) [perPage:protected] => 15 [exists] => 1 [wasRecentlyCreated] => [attributes:protected] => Array ( [name] => Constantina Wreight ) [original:protected] => Array ( [name] => Constantina Wreight ) [changes:protected] => Array ( ) [casts:protected] => Array ( ) [dates:protected] => Array ( ) [dateFormat:protected] => [appends:protected] => Array ( ) [dispatchesEvents:protected] => Array ( ) [observables:protected] => Array ( ) [relations:protected] => Array ( ) [touches:protected] => Array ( ) [visible:protected] => Array ( ) [guarded:protected] => Array ( [0] => * ) ) ) ) [32] => Illuminate\Database\Eloquent\Collection Object ( [items:protected] => Array ( [0] => App\User Object ( [table:protected] => users [fillable:protected] => Array ( ) [hidden:protected] => Array ( [0] => password ) [primaryKey:protected] => id [keyType:protected] => bigInteger [timestamps] => [remember_token] => [connection:protected] => mysql [incrementing] => 1 [with:protected] => Array ( ) [withCount:protected] => Array ( ) [perPage:protected] => 15 [exists] => 1 [wasRecentlyCreated] => [attributes:protected] => Array ( [name] => Clarissa Coomer ) [original:protected] => Array ( [name] => Clarissa Coomer ) [changes:protected] => Array ( ) [casts:protected] => Array ( ) [dates:protected] => Array ( ) [dateFormat:protected] => [appends:protected] => Array ( ) [dispatchesEvents:protected] => Array ( ) [observables:protected] => Array ( ) [relations:protected] => Array ( ) [touches:protected] => Array ( ) [visible:protected] => Array ( ) [guarded:protected] => Array ( [0] => * ) ) ) ) )
use first() instead of get() -
$buddylist[$value] = User::select('fullname as name')->where('id', $value)->first();
And in your view -
#foreach($buddylist as $buddy)
<ul>
<li>{{ $buddy->name }}</li>
</ul>
#endforeach
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] => *
)
)
)
)
I need to build a custom sort on an object array but i'm not sure where to start. I've seen the functions usort() etc but I can't see how i'd implement these functions for what I require.
My application is returning the below object array. I need to order it by the "slot" field in the following order: All Day, Morning, Afternoon.
Illuminate\Database\Eloquent\Collection Object
(
[items:protected] => Array
(
[0] => App\BookingDates Object
(
[connection:protected] =>
[table:protected] =>
[primaryKey:protected] => id
[perPage:protected] => 15
[incrementing] => 1
[timestamps] => 1
[attributes:protected] => Array
(
[id] => 265
[slot] => Morning
)
[original:protected] => Array
(
[id] => 265
[slot] => Morning
)
[relations:protected] => Array
(
)
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[appends:protected] => Array
(
)
[fillable:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
[dates:protected] => Array
(
)
[casts:protected] => Array
(
)
[touches:protected] => Array
(
)
[observables:protected] => Array
(
)
[with:protected] => Array
(
)
[morphClass:protected] =>
[exists] => 1
)
[1] => App\BookingDates Object
(
[connection:protected] =>
[table:protected] =>
[primaryKey:protected] => id
[perPage:protected] => 15
[incrementing] => 1
[timestamps] => 1
[attributes:protected] => Array
(
[id] => 266
[slot] => Afternoon
)
[original:protected] => Array
(
[id] => 266
[slot] => Afternoon
)
[relations:protected] => Array
(
)
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[appends:protected] => Array
(
)
[fillable:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
[dates:protected] => Array
(
)
[casts:protected] => Array
(
)
[touches:protected] => Array
(
)
[observables:protected] => Array
(
)
[with:protected] => Array
(
)
[morphClass:protected] =>
[exists] => 1
)
[2] => App\BookingDates Object
(
[connection:protected] =>
[table:protected] =>
[primaryKey:protected] => id
[perPage:protected] => 15
[incrementing] => 1
[timestamps] => 1
[attributes:protected] => Array
(
[id] => 267
[slot] => All Day
)
[original:protected] => Array
(
[id] => 267
[slot] => All Day
)
[relations:protected] => Array
(
)
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[appends:protected] => Array
(
)
[fillable:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
[dates:protected] => Array
(
)
[casts:protected] => Array
(
)
[touches:protected] => Array
(
)
[observables:protected] => Array
(
)
[with:protected] => Array
(
)
[morphClass:protected] =>
[exists] => 1
)
)
)
The result i'm looking for is this:
Illuminate\Database\Eloquent\Collection Object
(
[items:protected] => Array
(
[0] => App\BookingDates Object
(
[connection:protected] =>
[table:protected] =>
[primaryKey:protected] => id
[perPage:protected] => 15
[incrementing] => 1
[timestamps] => 1
[attributes:protected] => Array
(
[id] => 267
[slot] => All Day
)
[original:protected] => Array
(
[id] => 267
[slot] => All Day
)
[relations:protected] => Array
(
)
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[appends:protected] => Array
(
)
[fillable:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
[dates:protected] => Array
(
)
[casts:protected] => Array
(
)
[touches:protected] => Array
(
)
[observables:protected] => Array
(
)
[with:protected] => Array
(
)
[morphClass:protected] =>
[exists] => 1
)
[1] => App\BookingDates Object
(
[connection:protected] =>
[table:protected] =>
[primaryKey:protected] => id
[perPage:protected] => 15
[incrementing] => 1
[timestamps] => 1
[attributes:protected] => Array
(
[id] => 265
[slot] => Morning
)
[original:protected] => Array
(
[id] => 265
[slot] => Morning
)
[relations:protected] => Array
(
)
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[appends:protected] => Array
(
)
[fillable:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
[dates:protected] => Array
(
)
[casts:protected] => Array
(
)
[touches:protected] => Array
(
)
[observables:protected] => Array
(
)
[with:protected] => Array
(
)
[morphClass:protected] =>
[exists] => 1
)
[2] => App\BookingDates Object
(
[connection:protected] =>
[table:protected] =>
[primaryKey:protected] => id
[perPage:protected] => 15
[incrementing] => 1
[timestamps] => 1
[attributes:protected] => Array
(
[id] => 266
[slot] => Afternoon
)
[original:protected] => Array
(
[id] => 266
[slot] => Afternoon
)
[relations:protected] => Array
(
)
[hidden:protected] => Array
(
)
[visible:protected] => Array
(
)
[appends:protected] => Array
(
)
[fillable:protected] => Array
(
)
[guarded:protected] => Array
(
[0] => *
)
[dates:protected] => Array
(
)
[casts:protected] => Array
(
)
[touches:protected] => Array
(
)
[observables:protected] => Array
(
)
[with:protected] => Array
(
)
[morphClass:protected] =>
[exists] => 1
)
)
)
You can try something like this
public function index()
{
$bookingsDate = BookingDate::all() // Your actual collection
$this->sortCollection($bookingDate);
}
public function sortCollection(\Collection &$bookingDate)
{
$bookingDate->sortBy(function($date)
{
$slots = [
'Afternoon' => 1,
'Morning' => 2,
'All day' => 3,
];
$slot = $slots[$date['slot']];
return $slot;
});
}
The sortBy method will do an ascending sort, for descending sort, try sortByDesc
See : http://laravel.com/docs/5.1/collections#method-sortby
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...
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...