How to Search Data in JSON Field with Laravel Eloquent

Laravel has great support for JSON data. You can easily query and manipulate JSON data using Laravel’s Eloquent ORM. You can also use Laravel’s built-in JSON support to easily convert JSON data to and from PHP arrays.

However, there is one caveat when working with JSON data in Laravel. When you query a JSON column, you will not be able to use Eloquent’s usual where methods. This is because the JSON data is stored as a string in the database and not parsed into an array.

//use where
$term = 'Java';
$data = ProgrammingLanguage::where('language', 'like', '"%.$term.%"')->get();

//use json_contains
$keyword = 'Kotlin';
$language = ProgrammingLanguage::whereRaw('json_contains(language, \'["' . $keyword. '"]\')')->get();

//use whereJsonContains in Laravel 5.6+
$flutter = ProgrammingLanguage::whereJsonContains('language', ["Flutter"])->get();
$mobileLanguages = ProgrammingLanguage::whereJsonContains('language', ["Flutter", "React Native"])->get();

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close