dousikuai5417 2017-04-10 02:06 采纳率: 0%
浏览 30
已采纳

雄辩的ORM与多个模型

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?

展开全部

  • 写回答

2条回答 默认 最新

  • douxiongzhen2126 2017-04-10 04:15
    关注

    You can try like this:

    App\User::with('school', 'grades', 'lessons')->find(1)->toArray();
    

    You should get an array similar to one you wanted.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部