如何将此原始 SQL 查询转换为 Laravel Eloquent
How to transform this raw SQL query into Laravel Eloquent
这个原始 SQL 查询在我的 SQL 控制台上返回预期结果。你能帮我把它变成一个 Laravel Eloquent 查询吗?
SELECT * FROM `my_services`
WHERE `user_id` = 1 and `financial_year` = '2021-2022'
AND (service_type = 'Return' OR service_type = 'Correction Return')
ORDER BY id DESC LIMIT 1,1;
我试过像下面这样实现它。
MyService::where([
'user_id' => $user->id,
'financial_year' => $request->financial_year,
'financial_year' => '2021-2022'
])
->orWhere(['service_type' => 'Return'])
->orWhere(['service_type' => 'Correction Return'])
->orderBy("id", "desc")
->offset(1)
->limit(1)
->get();
发件人:https://laravel.com/docs/8.x/queries#limit-and-offset
使用 ->skip(1)
或 ->offset(1)
作为偏移量
使用->take(1)
或->limit(1)
限制返回结果的数量
试试这个查询 -
MyService::where('user_id', 1)->where('financial_year', '2021-2022')->where(function($q) {
$q->where('service_type', 'Return')->orWhere('service_type', 'Correction Return');
})->limit(1)->offset(1)->orderBy('id', 'DESC')->get();
这个原始 SQL 查询在我的 SQL 控制台上返回预期结果。你能帮我把它变成一个 Laravel Eloquent 查询吗?
SELECT * FROM `my_services`
WHERE `user_id` = 1 and `financial_year` = '2021-2022'
AND (service_type = 'Return' OR service_type = 'Correction Return')
ORDER BY id DESC LIMIT 1,1;
我试过像下面这样实现它。
MyService::where([
'user_id' => $user->id,
'financial_year' => $request->financial_year,
'financial_year' => '2021-2022'
])
->orWhere(['service_type' => 'Return'])
->orWhere(['service_type' => 'Correction Return'])
->orderBy("id", "desc")
->offset(1)
->limit(1)
->get();
发件人:https://laravel.com/docs/8.x/queries#limit-and-offset
使用 ->skip(1)
或 ->offset(1)
作为偏移量
使用->take(1)
或->limit(1)
限制返回结果的数量
试试这个查询 -
MyService::where('user_id', 1)->where('financial_year', '2021-2022')->where(function($q) {
$q->where('service_type', 'Return')->orWhere('service_type', 'Correction Return');
})->limit(1)->offset(1)->orderBy('id', 'DESC')->get();