0

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

2
  • what is this ->> operator? Commented Jul 11, 2017 at 5:09
  • @Tiger- I have update the question.please help me Commented Jul 11, 2017 at 5:24

2 Answers 2

1

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

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

Comments

0

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();

2 Comments

select * from Appraisal_commnds where Content->'$.""$"'.cycle_id" = 1 limit 1 query like this
Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.cycle_id" = ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.