user7540051
user7540051

Reputation:

How to convert query to laravel

I am totally new to laravel. I have a table

    +---------------------+
    | id | Content        |  
    +---------------------+
    |   1  | {"_token":"yAzxyLrdQfVscyc3fNB87PU4e1p6sS4JwX6AfmUQ","datefrom":"2017-07-07","dateto":"2017-07-31","Productivity":"Productivity","Productivityrating":"2","Technical_Skills":"Technical skill","Technical_Skillsrating":"3","Work_Consistency":"Work consistency","Work_Consistencyrating":"4","Presentation_skills":"Presentaion skills","Presentation_skillsrating":"3","test":"test","testrating":"5","cycle_id":"1","save":"proceed"}  | 
    |        
    |       
    |  
    +------+--------------+

field name Content is json. like

 {"_token":"yAzxyLrdQfVscyc3fNB87PU4e1p6sS4JwX6AfmUQ",
    "datefrom":"2017-07-07",
    "dateto":"2017-07-31",
    "Productivity":"Productivity",
    "Productivityrating":"2",
    "Technical_Skills":"Technical skill",
    "Technical_Skillsrating":"3",
    "Work_Consistency":"Work consistency",
    "Work_Consistencyrating":"4",
    "Presentation_skills":"Presentaion skills",
    "Presentation_skillsrating":"3",
    "test":"test",
    "testrating":"5",
    "cycle_id":"1",
    "save":"proceed"}

I want to Search from table where cycle_id=1 I have a mysql query

$sql="SELECT *from table where `Content`->>'$.cycle_id'=1";

How can i convert this to laravel?

like

$user = DB::table('table')->where('Content', '$.cycle_id'=1)->first();

Please help me.Any help would be appreciated

Upvotes: 0

Views: 44

Answers (2)

Pablor
Pablor

Reputation: 21

Since Laravel 5.6.24, you can use WhereJsonContains(): User::whereJsonContains('content', ['cycle_id' => 1])->get()

Upvotes: 1

Vishal Tarkar
Vishal Tarkar

Reputation: 826

First you need to create model.

You can use

php artisan make:model {TableName}

command to create one.

You can use this query.

$user = DB::table('table')->where('Content->"$.cycle_id"', '1')->first();

or

$user = TableName::where('Content->"$.cycle_id"', '1')->first();

Upvotes: 0

Related Questions