【问题标题】:Query Eloquent laravel查询 Eloquent laravel
【发布时间】:2017-10-22 20:21:33
【问题描述】:

我需要在 Eloquent Laravel 中转换这个 SQL 查询

Select * from modulo where idmodulo not in
(select idmodulo from moduloperfil where moduloperfil.idperfil = 2)

号码2$request

【问题讨论】:

  • 你的model..在哪里?
  • 你也可以使用whereNotIn
  • 模是我的模型
  • hooo 准备好了,这是我的解决方案 抱歉,请看 $result= \DB::table('modulo') ->whereNotIn('modulo.idmodulo',function ($query) use ($request ) { $query->select('moduloperfil.idmodulo')->from('moduloperfil') ->Where('moduloperfil.idperfil','=',$request->perfilselecionado); })->get()
  • 那甚至不是eloquent 也没有使用model - 它只使用 Laravel 的查询生成器。在询问之前,您可以在the docs 阅读更多内容。谢谢。

标签: php laravel laravel-5


【解决方案1】:

正如 cmets 中指出的,这不是 Eloquent

Eloquent 是 Laravel 使用的活动记录实现。

也就是说,如果您想使用 Laravel 的 Fluent 查询构建器,您可以执行以下操作:

DB::table('modulo')
    ->whereExists(function ($query) {
        $query->select(DB::raw(1))
            ->from('moduloperfil')
            ->where('moduloperfil.idperfil', '2')
            ->whereRaw('modulo.idmodulo = moduloperfil.idmodulo');
    })
->get();

上面使用whereExists 而不是whereIn 但应该给你相同的结果。

希望这会有所帮助!

【讨论】:

    【解决方案2】:

    你可以试试这个

    $result = Modulo::whereNotIn('idmodulo', function ($query) {
            $query->selectRaw('idmodulo from moduloperfil where idperfil = 2');
        })->get();
    

    【讨论】:

    • 请补充说明
    • 这将从moduloperfil中选择所有idmodulo,其中`idperfil' = 2,然后Modulo将找到除匹配值之外的所有结果。类似 Modulo::whereNotIn('idmodulo',[1,2,3,4])->get();匹配的数据将不包括在内。
    猜你喜欢
    • 2015-02-22
    • 2017-06-05
    • 1970-01-01
    • 1970-01-01
    • 2015-02-13
    • 1970-01-01
    • 2013-10-03
    • 2015-06-27
    • 2019-12-20
    相关资源
    最近更新 更多