未找到 Laravel OrderByRaw 列 [英] Laravel OrderByRaw column not found

查看:64
本文介绍了未找到 Laravel OrderByRaw 列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 orderByRaw Laravel 时遇到问题.

I Got a problem with orderByRaw Laravel.

数组:

$arr = [H123456, H7654321];

查询:

$ids = implode(',', $arr);
$query = User::whereIn('id', $arr)->isActive()->orderByRaw(DB::raw("FIELD(id,". $ids.")"))->get();

这是原始查询:

"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;

当我执行查询时,它说 Column not found: 1054 Unknown column 'Hf561b6fd32aec6ea' in 'order clause'.

When i execute the query it says Column not found: 1054 Unknown column 'Hf561b6fd32aec6ea' in 'order clause'.

我尝试使用单个值更改 $ids,例如 1 它可以工作.但如果 ID 是像我这样的字符串就行不通了.

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.

有什么解决办法吗?

推荐答案

由于 id 是字符串,所以需要对其进行封装.幸运的是,laravel 可以为您做到.试试这个:

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();

这篇关于未找到 Laravel OrderByRaw 列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆