如何使用Eloquent创建此查询?
SELECT
*
FROM
`contents`
WHERE
`id` NOT IN (
SELECT
`id`
FROM
`contents` AS `laravel_reserved_0`
WHERE
`laravel_reserved_0`.`parent_id` IN (
SELECT
`id`
FROM
`contents` AS `laravel_reserved_1`
WHERE
`laravel_reserved_1`.`type` IN ( 'pro' )
)
)
AND `is_active` = 1
AND `type` IN ( 'stars', 'video', 'article' )
GROUP BY
`slug`
ORDER BY
`created_at` DESC
LIMIT 10
答案 0 :(得分:0)
您可以执行多个查询,对于嵌套查询可以单独进行。
$typeIDs = Content::whereIn('type', ['pro'])->pluck('id');
$parentIDs = Content::whereIn('parent_id', $typeIDs)->pluck('id');
$contents = Content::whereNotIn('id', $parentIDs)
->where('is_active', 1)
->whereIn('type', ['stars', 'video', 'article'])
->groupBy('slug')
->orderBy('created_at', 'desc')
->limit(10)
->get();
此外,如果您愿意,还可以使用实际查询本身替换创建的变量。