如何将查询转换为laravel

时间:2017-07-11 05:05:36

标签: php mysql laravel-5.4

我对laravel完全不熟悉。我有一张桌子

    +---------------------+
    | 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"}  | 
    |        
    |       
    |  
    +------+--------------+

字段名称内容为json。像

 {"_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"}

我想从表中搜索cycle_id = 1 我有一个mysql查询

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

我如何将其转换为laravel?

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

请帮帮我。欢迎任何帮助

2 个答案:

答案 0 :(得分:1)

从Laravel 5.6.24开始,您可以使用WhereJsonContains(): User::whereJsonContains('content', ['cycle_id' => 1])->get()

答案 1 :(得分:0)

首先,您需要创建模型。

您可以使用

php artisan make:model {TableName}

命令创建一个。

您可以使用此查询。

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

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