Laravel json column query to check if object exists

Viewed 8163

So I have a json column called settings in the table companies (which has a Laravel model called Company). That column may or may not contain an object called business_key. I try to return only the companies that contain this object. My query looks like this:

$owners = \Company::where('settings->business_key')->get();

I don't know how to make something like this work. I did not find anything Laravel specific for this. Thank you for your time and help!

3 Answers

In the purpose to retrieve all not null data, you can try this :

$owners = \Company::whereRaw('JSON_EXTRACT(settings, "$.business_key") <> CAST("null" AS JSON)')->get();
Related