0

For example, I have a column named json in table A

Json column contains json data like this :

record 1 : {"dept_code": "012", "unit_code": "22"}
record 2 : {"dept_code": "013", "unit_code": "23"}
etc

I want to take the data records with json column that contain dept_code = 012

I try like this :

$id = "012";
$data = \DB::table('table_A')
           ->select('*')
           ->where(JSON_EXTRACT('json', "$.dept_code"), '=', '"'.$id.'"')
           ->get();

There exist error like this :

Call to undefined function App\Http\Controllers\JSON_EXTRACT()

How can I resolve that?

1 Answer 1

1

You can use laravel's JSON where clauses. E.g.

$id = "012";
$data = DB::table('table_A')
           ->where('json->dept_code', $id)
           ->get();

This works in Postgres and MySQL(since 5.7.8)

Query Builder Where Clauses

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.