V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
lingxueyu
V2EX  ›  PHP

预载入查询构造怎么做?

  •  
  •   lingxueyu · 2015-04-10 15:13:48 +08:00 · 3057 次点击
    这是一个创建于 3309 天前的主题,其中的信息可能已经有所发展或是发生改变。
    laravel文档对于ORM的预载入内容很少,使用中发现貌似只有预载入条件限制才起作用
    $users = User::with(array('posts' => function($query)
    {
    $query->where('title', 'like', '%first%');

    }))->get();
    而对于select/lists这些总是返回空
    $company = Company::with(array('users' => function($query)
    {
    $query->select('id');
    }))->select('id', 'type', 'logo', 'name', 'profile', 'city', 'email', 'phone', 'website', 'weibo', 'tags')->find($id);

    返回的$company的json一直是
    {
    "status": "success",
    "data": {
    "id": 1,
    "type": 0,
    "logo": "1428470471LLnfH.jpg",
    "name": "123",
    "profile": "",
    "city": "",
    "email": "[email protected]";,
    "phone": "123456",
    "website": "",
    "weibo": "",
    "tags": "",
    "user_count": 3,
    "thumb_logo": "http://120.25.210.83/upload/companies/thumb/1428470471LLnfH.jpg",
    "users": []
    }
    }
    users总是为空,但是换成$query->where才能返回数据
    要怎么使select也在预载入起作用?
    以及如果users数组里面还包含了tweets数组,怎么做tweets的预载入?
    1 条回复    2016-01-26 21:52:26 +08:00
    qljiang
        1
    qljiang  
       2016-01-26 21:52:26 +08:00
    请问第一个问题你现在解决了吗?是怎么解决的呢?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1017 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 23:37 · PVG 07:37 · LAX 16:37 · JFK 19:37
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.