Laravel 获取 JSON 列 MariaDB 语法错误
Laravel fetching JSON column MariaDB syntax error
我正在尝试根据 JSON 列 meta
获取一些数据。但是,->
符号周围发生了一些奇怪的事情。
File::whereJsonContains('meta->serie', 'value')->toSql();
输出
"select * from `files` where json_contains(`meta`->'$.\"serie\"', ?)"
这是我得到的错误
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '>'$."serie"', ?)' at line 1 (SQL: select * from files
where json_contains(meta
->'$."serie"', "check_up"))
架构
class File extends Model {
protected $fillable = ['filename', 'mime-type', 'path', 'meta', 'type'];
protected $casts = [
'meta' => 'array'
];
const PATH = 'files';
public function uploadable() {
return $this->morphTo();
}
public function receivable() {
return $this->morphTo();
}
}
控制器
class FilesController extends Controller {
public function download(Request $request) {
$data = $this->validate($request, [
'type' => 'required|alpha_dash',
'meta' => 'sometimes|required',
]);
$search = [
['type', $data['type']],
];
if ($request->has('meta')) {
foreach (json_decode($data['meta']) as $key => $value) {
$search[] = ["meta->$key", 'like', $value];
}
}
$files = File::where($search)->get();
return response()->json($files);
}
}
我尝试使用常规 where
,但它会引发相同的错误。有什么想法吗?
试试这个:
$data = $request->all();
$query = File::where('type', $data['type']);
if ($request->has('meta')) {
foreach (json_decode($data['meta']) as $key => $value) {
$query->whereRaw("JSON_CONTAINS(meta, '\"{$value}\"', '$.{$key}')");
}
}
$files = $query->get();
我正在尝试根据 JSON 列 meta
获取一些数据。但是,->
符号周围发生了一些奇怪的事情。
File::whereJsonContains('meta->serie', 'value')->toSql();
输出
"select * from `files` where json_contains(`meta`->'$.\"serie\"', ?)"
这是我得到的错误
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '>'$."serie"', ?)' at line 1 (SQL: select * from
files
where json_contains(meta
->'$."serie"', "check_up"))
架构
class File extends Model {
protected $fillable = ['filename', 'mime-type', 'path', 'meta', 'type'];
protected $casts = [
'meta' => 'array'
];
const PATH = 'files';
public function uploadable() {
return $this->morphTo();
}
public function receivable() {
return $this->morphTo();
}
}
控制器
class FilesController extends Controller {
public function download(Request $request) {
$data = $this->validate($request, [
'type' => 'required|alpha_dash',
'meta' => 'sometimes|required',
]);
$search = [
['type', $data['type']],
];
if ($request->has('meta')) {
foreach (json_decode($data['meta']) as $key => $value) {
$search[] = ["meta->$key", 'like', $value];
}
}
$files = File::where($search)->get();
return response()->json($files);
}
}
我尝试使用常规 where
,但它会引发相同的错误。有什么想法吗?
试试这个:
$data = $request->all();
$query = File::where('type', $data['type']);
if ($request->has('meta')) {
foreach (json_decode($data['meta']) as $key => $value) {
$query->whereRaw("JSON_CONTAINS(meta, '\"{$value}\"', '$.{$key}')");
}
}
$files = $query->get();