Laravel OrderByRaw Column Not Found
I Got a problem with
$arr = [H123456, H7654321];
$ids = implode(',', $arr); $query = User::whereIn('id', $arr)->isActive()->orderByRaw(DB::raw("FIELD(id,". $ids.")"))->get();
This is the raw query:
"select * from `merchant_heads` where `id` in (?, ?) and `category_id` = ? and `status` = ? order by FIELD(id,Hf561b6fd32aec6ea,H7c81e6fa3f85fc74) limit 10 offset 0"
I've already put this inside my User model:
public $incrementing = false;
When i execute the query it says
Column not found: 1054 Unknown column 'Hf561b6fd32aec6ea' in 'order clause'.
I've tried to change $ids with single value like
1 it working. but it's not working if the ID is string like mine.
Since the ids are strings, you need to encapsulate them. Luckily, laravel can do it for you. Try with this:
$ids = implode(',', $arr); $qs = array_fill(0,count($arr),'?'); $query = User::whereIn('id', $arr)->isActive()->orderByRaw(DB::raw("FIELD(id,". implode(',', $qs).")"),$arr)->get();
- → "failed to open stream" error when executing "migrate:make"
- → October CMS Plugin Routes.php not registering
- → OctoberCMS Migrate Table
- → OctoberCMS Rain User plugin not working or redirecting
- → October CMS Custom Mail Layout
- → October CMS - How to correctly route
- → October CMS create a multi select Form field
- → October CMS - Conditionally Load a Different Page
- → How to disable assets combining on development in OctoberCMS
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → OctoberCms component: How to display all ID(items) instead of sorting only one ID?
- → In OctoberCMS how do you find the hint path?
- → How to register middlewares in OctoberCMS plugin?