P
P
PRIZRAKeee2018-03-16 11:35:37
Laravel
PRIZRAKeee, 2018-03-16 11:35:37

How to write such a SQL query in Laravel?

There is a query that works if you use it in phpmyadmin.

(SELECT * FROM products WHERE brand = 'Atlas Copco' ORDER BY created_At DESC) UNION (SELECT * FROM products WHERE brand != 'Atlas Copco' ORDER BY created_at DESC)

But I can't write it for Laravel in eloquent orm.
I tried to do it like this, but it gives an error
$products_atlas_copco = $category->products()->whereBrand('Atlas Copco')->orderBy('created_at', 'desc')->public();
$products = $category->products()->where('brand','!=','Atlas Copco')->orderBy('created_at', 'desc')->public()->union($products_atlas_copco);

But individually both queries work if I do this
$products_atlas_copco = $category->products()->whereBrand('Atlas Copco')->orderBy('created_at', 'desc')->public();
$products = $category->products()->where('brand','!=','Atlas Copco')->orderBy('created_at', 'desc')->public();

Tell me please. where am i making a mistake

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question