0

样本数据:

13 => {#531 ▼
        +"id": 956
        +"user_type": "App\User"
        +"user_id": 35
        +"event": "updated"
        +"auditable_type": "App\Video"
        +"auditable_id": 136
        +"old_values": "{"video_status_ids":"[4, 16]"}"
        +"new_values": "{"video_status_ids":"[5,16]"}"
        +"url": "http://example.com?vhjb6gsyyas"
        +"ip_address": "106.51.36.44"
        +"user_agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36"
        +"tags": null
        +"created_at": "2019-01-14 11:53:09"
        +"updated_at": "2019-01-14 11:53:09"
      }

代码:

 $audits = DB::table('audits')->where('auditable_type', 'LIKE', 'App%Video')->where('new_values', 'LIKE', '{"video_status_ids":"%')->where(function($query) {
        $query->whereJsonDoesntContain('old_values->video_status_ids', '4')
            ->WhereJsonDoesntContain('new_values->video_status_ids', '5');
    })->paginate(200);

日志文件中记录的 SQL 查询为:

array (
    'query' => 'select * from `audits` where `auditable_type` LIKE 'App%Video' and `new_values` LIKE '{"video_status_ids":"%'and (not json_contains(`old_values`->\'$."video_status_ids"\', '"[1]"') and not json_contains(`new_values`->\'$."video_status_ids"\', '"[2]"')) and (not json_contains(`old_values`->\'$."video_status_ids"\', '4') or not json_contains(`new_values`->\'$."video_status_ids"\', '5') and not json_contains(`old_values`->\'$."video_status_ids"\', '7') or not json_contains(`new_values`->\'$."video_status_ids"\', '8') and not json_contains(`old_values`->\'$."video_status_ids"\', '10') or not json_contains(`new_values`->\'$."video_status_ids"\', '11') and not json_contains(`old_values`->\'$."video_status_ids"\', '13') or not json_contains(`new_values`->\'$."video_status_ids"\', '14') and not json_contains(`old_values`->\'$."video_status_ids"\', '16') or not json_contains(`new_values`->\'$."video_status_ids"\', '17')) limit 200 offset 0',
    'bindings' => 
    array (
      0 => 'App%Video',
      1 => '{"video_status_ids":"%',
      2 => '"[1]"',
      3 => '"[2]"',
      4 => '4',
      5 => '5',
      6 => '7',
      7 => '8',
      8 => '10',
      9 => '11',
      10 => '13',
      11 => '14',
      12 => '16',
      13 => '17',
    ),

根据我想要获得的记录,有一种组合模式。像上面的代码一样,我想要其中“old_values->video_status_ids不包含 4new_values->video_status_ids且不包含 5”的记录,如果有其他组合而不是 4-5,例如 4-6 或 4-7,它们仍然应该被检索。

更新 在原始查询中,它将类似于:

SELECT * FROM audits WHERE JSON_CONTAINS(new_values->>'$.video_status_ids', '5') AND NOT JSON_CONTAINS (old_values->>'$.video_status_ids', '4') 

现在我想要它在 laravel 查询生成器中。

4

0 回答 0