Support the ongoing development of Laravel.io →
Database Eloquent

Hi, frinds Can U help me in my case with eloquent subquery? )

I have the code in my model:

return News::select(['id', 'title'])
    ->whereIn('id', function ($sub) {
        $sub->from('news_tags')
            ->select('news_id as id')
            ->whereIn('tag_id', $this->tagsIn([1])->pluck('id'))
            ->groupBy('news_id');
        })
    ->whereStatus(1)
    ->orderBy('published', 'DESC')
    ->take(3)
    ->get();

On the first PC i have correct SQL request:

select `id`, `title`
from `news`
where `id` in (select `news_id` as `id` from `news_tags` where `tag_id` in ('2198', '2424', '2462', '2214', '2203') group by `news_id`) and `status` = '1'
order by `published` desc
limit 3

On the second PC, subquery returns incorrect SQL request, like this:

select `id`, `title`
from `news`
where `id` in (select `news_id` as `id` from `news_tags` where `tag_id` in 0 = 1 group by `news_id`) and `status` = '1'
order by `published` desc
limit 3

What is the reason? What incorrectly executes a query on a second machine?

I heed your help... )

Last updated 2 years ago.
0

Can you show us what $this->tagsIn([1])->pluck('id') looks like before each query?

0

Sign in to participate in this thread!

Eventy

Your banner here too?

FATER fater Joined 26 Jan 2016

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.