Back

The Eloquent subquery is not executed correctly


FATER posted 8 months ago

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... )

whatarewe replied 7 months ago

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


Sign in to participate in this thread!



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