Tuesday, April 11, 2017

Eloquent ORM with multiple models

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:

  • User
  • School
  • Grade
  • Lesson

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?



via Ventilaator

Advertisement