Y
Y
Yurka Blokhin2017-07-06 08:51:00
Laravel
Yurka Blokhin, 2017-07-06 08:51:00

How to make a union request in laravel so that pagination works?

Good afternoon, I'm trying to make a request on laravel in order to get products from child categories and the category itself in the output. Here is the request itself:

$first = Product::where('status', 1)
->where('category_id', $catalog->id);

$load_product = Product::where('status', 1)
->whereIn('category_id', Catalog::select('id')->where('parent_id', $catalog->id)->get())
->union($first)
->paginate();

in response I get:
SQLSTATE[21000]: Cardinality violation: 1222 The used SELECT statements have a different number of columns (SQL: (select count(*) as aggregate

if at the end of the request instead of ->paginate(); write ->get(); then everything works fine.
How to make pagination work correctly?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2017-07-06
@blrik

Why is union here? After all, you can do everything with one request, something like this:

$load_product = Product::where('status', 1)->where(function($query) use($catalog)
{
    return $query->where('category_id', $catalog->id)
           ->orWhereIn('category_id', Catalog::where('parent_id', $catalog->id)->pluck('id'));
}
->paginate();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question