5

Here is json array

"section_4": {
"data": [
{
"grade": "3.0",
"degree": "BSCSE",
"end_date": "2019-03-28T10:35:00.000Z",
"institute": "DIU",
"start_date": "2019-03-25T10:35:00.000Z"
},
{
"grade": "4.0",
"degree": "BSCSE",
"end_date": "2019-04-30T10:50:00.000Z",
"institute": "STU",
"start_date": "2019-03-31T10:50:00.000Z"
}
],
"type": "education",
"title": "Educations"
},

Want to search with grade, is it possible to search with query builder?

$resumes = UserMeta::with(['user.resume' => function ($query) {
                $query->where('data->section_4->type', 'like', '%education%');
        }])->paginate(10);

Here is code to try to search in controller. is it possible to search with grade with query builder laravel?

2 Answers 2

5

For search in json array in laravel you can use this : for example you want check value in "grade" from "data" array :

$resumes = UserMeta::with(['user.resume' => function ($query) {
            $query->whereJsonContains('data->data', ['grade' => "3.0"]);
    }])->paginate(10);

Done....

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

1 Comment

Thanks a lot for this! it works! Do you have a way to use like with this method ?
2

Laravel offers whereJsonContains so you can try the below code:

$resumes = UserMeta::with(['user.resume' => function ($query) {
                $query->whereJsonContains('data->section_4->type', 'like', '%education%');
        }])->paginate(10);

OR you can also use whereRaw with the combination of JSON_CONTAINS in this case:

e.g

whereRaw("JSON_CONTAINS(type, '[education]' )")->get();

For more check this post

5 Comments

Here got all the array object of education->data; want to search data->grade and institute
Got education data. Can i search with every grade and institute? @iftikhar uddin
@ShunhraSarker yes why not! It should be something like data->section_4->data['grade'] or data->section_4->data->grade
Here data can be multiple inside education. Is it work for multiple data without foreach loop?
@ShunhraSarker How did you manage to solve the loop?

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.