I'm having some issues with the Laravel Collection class.
What I'm trying to do:
- I have a multisite solution in which a site has "facilitators". Sometimes one facilitator appears on multiple sites, and not just one.
- I want to list all the facilitators and the website they're on the main page, but I don't want multiple users.
So what I currently do is:
- Get the Facilitators.
- Use Collection to collect the facilitators and use
unique('name')
.
This gives me unique facilitators, but only picks the first one it detects and then deletes the other ones.
So lets say I have this collection:
Collection {
#items: array:3 [
0 => array:2 [
"name" => "John"
"site" => "Example"
]
1 => array:2 [
"name" => "Martin"
"site" => "Another"
]
2 => array:2 [
"name" => "John"
"site" => "Another"
]
]
}
With unique()
I would get:
Collection {
#items: array:3 [
0 => array:2 [
"name" => "John"
"site" => "Example"
]
1 => array:2 [
"name" => "Martin"
"site" => "Another"
]
]
}
And this is what I want to get:
Collection {
#items: array:3 [
0 => array:2 [
"name" => "John"
"site" => ["Example", "Another"]
]
1 => array:2 [
"name" => "Martin"
"site" => "Another"
]
]
}
Does anyone have an idea how I could accomplish this with Laravel's collection class?
via Melvin Koopmans