Я пытаюсь отсортировать результаты по значению столбца, но это не работает
$users = Comment::select([
'id',
'comment',
'user_name',
'product_id',
'rating',
'country',
'status',
'pin',
'created_at',
])->where('shop_name',$shop)->where('product_id', $id)->with('images')->orderByRaw("IF(product_url = 'customer') DESC")->orderByRaw("product_url = manually ASC")->orderBy('pin', 'desc')->orderBy('rating', 'desc')->with('pages')->get();
Я добавил этот код
->orderByRaw("IF(product_url = 'customer') DESC")
и я получаю эту ошибку
"SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') DESC, product_url = manually ASC,
pindesc,ratingdesc' at line 1 (SQL: selectid,comment,user_name,product_id,rating,country,status,pin,created_atfromcommentswhereshop_name= andproduct_id= order by IF(product_url = 'customer') DESC, product_url = manually ASC,pindesc,ratingdesc)
Вы можете включить результат IF в вычисляемый столбец, а затем заказать его.






Функция MySQL IF принимает аргументы три.
Это выражение недействительно:
IF(product_url = 'customer')
потому что функции IF() предоставлен только один аргумент.
Мы могли сделать это:
IF(product_url = 'customer',1,0)
что эквивалентно более совместимому со стандартами ANSI
CASE WHEN product_url = 'customer' THEN 1 ELSE 0 END
Сокращение MySQL также подойдет
ORDER BY product_url = 'customer' DESC
что было бы эквивалентно
ORDER BY CASE
WHEN product_url = 'customer' THEN 1
WHEN product_url IS NOT NULL THEN 0
ELSE NULL
END DESC
проверьте stackoverflow.com/questions/29101242/…