I'm trying to query a MySQL database table which contains a JSON column.
In MySQL the following query runs
mysql> select members from `conversations` where `members` = CAST('[1,2,3]' AS JSON) limit 3;
And I'm able to see the result from mysql console.
+-----------+
| members |
+-----------+
| [1, 2, 3] |
| [1, 2, 3] |
| [1, 2, 3] |
+-----------+
However, when I use a Laravel QueryBuilder to build this query. It throws grammar error.
The Laravel code (inside a controller):
$memberList = $request->input('members');
DB::table("conversations")->where('members', '=', DB::raw('CAST(\''.$memberList.'\'AS JSON'))->get();
The error message:
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 '' at line 1 (SQL: select * from `conversations` where `members` = CAST('[1,2,3]'AS JSON)
Could you please help me know where am I doing wrong?
DB::raw('CAST('.$memberList.' AS JSON')