Home > front end >  What is the CROSS APPLY OPENJSON query for the laravel?
What is the CROSS APPLY OPENJSON query for the laravel?

Time:12-30

I am using this query.

In database structure column_name into Json format like this :

[
    {
        "Date": "2022-11-14T10:45:05.424 01:00",
        "Name": "nilesh1",
        "Level": "1"
    },
    {
        "Date": "2022-11-14T10:54:15.776 01:00",
        "Name": "nilesh2",
        "Level": "2"
    }
]

I have to use below query with Laravel eloquent:

select [customername] as [customerName]
from [opf_details] 
CROSS APPLY OPENJSON (column_name,'$') where JSON_VALUE(value,'$.Name') LIKE '%nilesh1%'

CodePudding user response:

I have done like this:

myModel::whereRaw('JSON_CONTAINS(`column_name`, '\{"Name":"nilesh2"}\')')

CodePudding user response:

you can use whereRaw() with the crossJoin()

$results = DB::table('opf_details')
    ->select('customername as customerName')
    ->whereRaw("JSON_VALUE(column_name, '$.Name') LIKE '%nilesh1%'")
    ->crossJoin(DB::raw("OPENJSON(column_name, '$')"))
    ->get();
  1. crossJoin()
  2. whereRaw()

Edit

$customerName = DB::table('opf_details')
    ->selectRaw('customername as customerName')
    ->crossJoin(DB::raw("OPENJSON (column_name,'$')"))
    ->whereRaw("JSON_VALUE(value,'$.Name') LIKE '%nilesh1%'")
    ->get();
  • Related