duanhe6799 2017-08-20 22:40
浏览 44
已采纳

Laravel - 多个急切加载关系的多个查询功能

I'm wondering if there's a solution to a multiple closure loading two hasOne relationships. With one relationship (personaldata) I only needed some columns, and second relationship (userfile) having a where clause. It works if I either remove the select from personaldata or remove the where clause from userfile, otherwise having two closures renders one of them with null value.

 $user = Users::with(['personaldata' => function($a) {
                    $a->select('first_name', 'title_name', 'last_name', 'street_address', 'city', 'state', 'country_code', 'zip_code', 'telephone_data', 'skype', 'per_street_address', 'per_city', 'per_state', 'per_country_code', 'per_zip_code');
                },
                'userfile' => function($b) {
                    $b->where('type', '1');
                }
                ])
                ->where('user_id', Auth::id())
                ->get();
  • 写回答

1条回答 默认 最新

  • donglvchu9143 2017-08-20 22:46
    关注

    change your code like this :

    $user = Users::with(['personaldata' => function($query) {
                $query->select('first_name', 'title_name', 'last_name', 'street_address', 'city', 'state', 'country_code', 'zip_code', 'telephone_data', 'skype', 'per_street_address', 'per_city', 'per_state', 'per_country_code', 'per_zip_code');
            }])
            ->with(['userfile' => function($query) {
                $query->where('type', '1');
            }])
            ->where('user_id', Auth::id())
            ->get();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效