【问题标题】:Convert Query Builder query to Eloquent ORM将查询生成器查询转换为 Eloquent ORM
【发布时间】:2015-07-21 11:12:24
【问题描述】:

我想将我当前的查询转换为ORM。但是,我不能清楚地了解如何加入。

我当前的查询:

        $result=DB::table('tests')
            ->join('tests_matchs', 'tests_matchs.matched_tests_id', '=', 'tests.id')
            ->where('tests_matchs.user_id', '=', $UserId)
            ->where('tests_matchs.passed', '=', 'true')
            ->get();

【问题讨论】:

    标签: mysql laravel laravel-4 orm laravel-5


    【解决方案1】:

    假设您的关系设置正确,这应该可以工作:

    $result = Test::with('tests_matchs' => function($query) user ($UserId){ 
        $query->where('user_id', '=', $UserId)
            ->where('passed', '=', 'true');
    })->get()
    

    【讨论】:

      【解决方案2】:

      @Pawel Bieszczad 你忘记了将数组作为参数,所以正确的查询应该是

      $result = Test::with(['tests_matchs' => function($query) uses ($UserId){ 
                 $query->where('user_id', '=', $UserId)
                       ->where('passed', '=', 'true');
      }])->get()
      

      【讨论】:

        猜你喜欢
        • 2021-10-14
        • 2020-07-09
        • 2020-10-13
        • 2018-08-06
        • 1970-01-01
        • 2017-03-10
        • 1970-01-01
        • 2015-12-13
        • 1970-01-01
        相关资源
        最近更新 更多