将查询生成器查询转换为Eloquent ORM

时间:2022-12-12 20:18:39

I want to convert my current query to ORM. But, I cant understand clearly how to join.

我想将当前查询转换为ORM。但是,我无法清楚地知道如何加入。

My current query:

我目前的查询:

        $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();

2 个解决方案

#1


Assuming your relationship is set correctly this should work:

假设您的关系设置正确,这应该有效:

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

#2


@Pawel Bieszczad you forgot that with takes array as argument, so the correct query would be

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

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

#1


Assuming your relationship is set correctly this should work:

假设您的关系设置正确,这应该有效:

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

#2


@Pawel Bieszczad you forgot that with takes array as argument, so the correct query would be

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

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