Laravel Eloquent Query MySQL JSON Column with LIKE

In order to query serialized JSON data from MySQL’s JSON column using Laravel Eloquent, first we need to define cast property for the serialized JSON attribute in a model as below:

class Product extends Model
{

    protected $casts = [
        'meta' => 'json'
    ];
...
...
}

Then, use the following search query in the controller.

$keyword = strtolower($request->get('keyword'));
$product = Product::whereRaw('LOWER(meta->"$.test_field") LIKE ?', '%'.$keyword.'%');

Leave a Reply

Your email address will not be published. Required fields are marked *