doubu1964 2015-12-12 12:21
浏览 11

雄辩在哪里与封闭

I want to do a whereIn with a closure. Is this possible?

Normally, you would go and create it like this

return User::whereIn('id', [1, 2, 3])
            ->whereLang($channel)
            ->get();

But, what I wanted to do, is to do it with a closure - if possible. I have a database field, which has ids of users as a string (The table design is not mine, so I can't change it currently and have to work with it);

So I tried to do it like this

return User::whereIn('id', function () {
    return FooBar::whereFooId('u' . $user->id)->get(['users'])->toArray();
})
    ->whereLang($lang)
    ->get();

However, when doing that, I'm getting the error

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000]: General error: 1096 No tables used'

I mean, in the database table I have the string with the ids stored like 1, 2, 3, 4, so I would just need to do an explode(', ', $ids);. This would solve my problem, when I do it in two steps, I was just wondering if I can do it in one, with the closure. And if so, how?

  • 写回答

1条回答 默认 最新

  • doujue6196 2015-12-12 13:14
    关注

    You probably want to do.

    return User::whereIn('id', FooBar::lists('uid'));
    

    Assuming the user ids are stored in uid field in FooBar table

    评论

报告相同问题?

悬赏问题

  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 正弦信号发生器串并联电路电阻无法保持同步怎么办
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)