I'm trying to flatten a collection in Laravel 5.2 to show all the unique values from a linked table in a single row. I have three tables, Users, Organisations and User_organisation which is a linking table. Users can belong to multiple Organisations.
When joining I get the following
"id" "name" "surname" "email" "organisation"
"2001" "Jim" "Tester1" "jim@example.com" "ABC"
"2001" "Jim" "Tester1" "jim@example.com" "XYZ"
"2001" "Jim" "Tester1" "jim@example.com" "123"
"2004" "Bob" "Smith" "bob@example.com" "ABC"
"2004" "Bob" "Smith" "bob@example.com" "MNO"
"2006" "Tom" "Foo" "tom@example.com" "MNO"
But I want to transform this to the following: single row for each User listing their Organisations.
"2001" "Jim" "Tester1" "jim@example.com" "ABC", "XYZ", "123"
"2004" "Bob" "Smith" "bob@example.com" "ABC", "MNO"
"2006" "Tom" "Foo" "tom@example.com" "MNO"
It would be even nicer if the columns aligned like so:
"2001" "Jim" "Tester1" "jim@example.com" "ABC", "XYZ", "123",
"2004" "Bob" "Smith" "bob@example.com" "ABC", , , "MNO"
"2006" "Tom" "Foo" "tom@example.com" , , , "MNO"
I don't mind if this is done with SQL or in php. I'm trying to avoid using lots of loops and was hoping for an elegant solution using the collection methods.
from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1XCGesV
via IFTTT
Aucun commentaire:
Enregistrer un commentaire