Reputation: 50
I'm trying to create a new web application, currently I have created a static array with some information which looks like this:
array(5) {
["id"]=> int(1)
["firstname"]=> string(6) "martin"
["lastname"]=> string(1) "r"
["school"]=> array(3) {
["id"]=> string(1) "3"
["name"]=> string(22) "tallinna polütehnikum"
["role"]=> string(7) "student"
}
["lessons"]=> array(2) {
[0]=> array(3) {
["id"]=> int(1)
["name"]=> string(10) "eesti keel"
["grades"]=> array(2) {
[0]=> array(2) {
["id"]=> int(1)
["grade"]=> int(4)
}
[1]=> array(2) {
["id"]=> int(3)
["grade"]=> int(5)
}
}
}
[1]=> array(3) {
["id"]=> int(2)
["name"]=> string(11) "matemaatika"
["grades"]=> array(1) {
[0]=> array(2) {
["id"]=> int(2)
["grade"]=> int(3)
}
}
}
}
}
Also I have models like this:
Lessons table has school_id foreign key.
School table has admin_id foreign key.
Grades table has lesson_id, teacher_id, user_id foreign keys.
I'm having troubles generating a dynamic array like I showed before. I tried using
->with('grades')->with('lessons')
but it didn't do anything like I wanted to.
Maybe someone can give me some directions?
Upvotes: 0
Views: 148
Reputation: 256
You can try like this:
App\User::with('school', 'grades', 'lessons')->find(1)->toArray();
You should get an array similar to one you wanted.
Upvotes: 1