【发布时间】:2021-09-11 15:05:43
【问题描述】:
这个原始 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();
【问题讨论】:
-
MyService::where('user_id', $user->id) ->where('financial_year', $request->financial_year) ->where(function($q) { $q- >orWhereIn('service_type', ['Return', '修正返回']); })->orderBy("id", "desc")->skip(1)->take(1)->get() ;
标签: laravel eloquent laravel-query-builder