#mysql #laravel
#mysql #laravel
Вопрос:
У меня есть запрос, подобный этому
SELECT id,image1 image FROM product_catalog
WHERE id='1' AND image1<>''
UNION ALL
SELECT id,image2 image FROM product_catalog
WHERE id='1' AND image2<>''
UNION ALL
SELECT id,image3 image FROM product_catalog
WHERE id='1' AND image3<>''
UNION ALL
SELECT id,image4 image FROM product_catalog
WHERE id='1' AND image4<>''
Но моя проблема в том, что я не могу преобразовать в построение запросов laraevl, поэтому есть идеи, как преобразовать мой запрос??
Я просто пробую эти :
$img1 = Product_catalog::select('id','image2 as image')->from('product_catalog')->where('id', '=', $id)->where('image2', '<>', '');
$img2 = Product_catalog::select('id','image3 as image')->from('product_catalog')->where('id', '=', $id)->where('image3', '<>', '')->unionAll($img1);
$img3 = Product_catalog::select('id','image4 as image')->from('product_catalog')->where('id', '=', $id)->where('image4', '<>', '')->unionAll($img2);
$imgall = Product_catalog::select('id','image1 as image')->from('product_catalog')->where('id', '=', $id)->where('image1', '<>', '')->unionAll($img3)->get();
Но не работает. Есть идеи?
ПРИМЕЧАНИЕ: я использую mysql для построения запросов
Комментарии:
1. Переместите все
->unionAll
в последний оператор.2. и как получить все объединения, такие как img1, img2, img3 и img4??
3. Очевидно
$imgall = Product_catalog::select('id','image1 as image')->from('product_catalog')->where('id', '=', $id)->where('image1', '<>', '')->unionAll($img1)->unionAll($img2)->unionAll($img3)->get();