如何正确使用where,whereJsonDoesntContain和whereJsonContains的多个组合

时间:2018-12-26 13:28:56

标签: php laravel eloquent

PHP版本7.2.10 Laravel版本5.6.39

我有这样的东西:

$statuses = [4, 5, 6, 7, 14, 15, 16, 17, 18, 19];
$notStatues = [8, 9, 10, 11, 12, 13];

$videos = Video::query();

if ( isset( $queryParams['category'] ) ){
    $videos = $videos->where('video_category', '=', $queryParams['category']);
}
if ( isset( $queryParams['language'] ) ){
    $videos = $videos->where('video_language', '=', $queryParams['language']);
}

//  $videos = $videos->jsonNotContains($statuses);

$videos = $videos->whereJsonContains('video_status_ids', $statuses[0])
    ->orWhereJsonContains('video_status_ids',  $statuses[1])
    ->orWhereJsonContains('video_status_ids',  $statuses[2])
    ->orWhereJsonContains('video_status_ids',  $statuses[3])
    ->orWhereJsonContains('video_status_ids',  $statuses[4])
    ->orWhereJsonContains('video_status_ids',  $statuses[5])
    ->orWhereJsonContains('video_status_ids',  $statuses[6])
    ->orWhereJsonContains('video_status_ids',  $statuses[7])
    ->orWhereJsonContains('video_status_ids',  $statuses[8])
    ->orWhereJsonContains('video_status_ids',  $statuses[9]);

$videos->whereJsonDoesntContain('video_status_ids', $notStatues[0])
    ->whereJsonDoesntContain('video_status_ids', $notStatues[1])
    ->whereJsonDoesntContain('video_status_ids', $notStatues[2])
    ->whereJsonDoesntContain('video_status_ids', $notStatues[3])
    ->whereJsonDoesntContain('video_status_ids', $notStatues[4])
    ->whereJsonDoesntContain('video_status_ids', $notStatues[5]);

$videos = $videos->orderBy('video_priority', 'desc')->paginate(10);

return $videos; 

实现它的更好方法是什么,对于当前代码,我什至无法过滤出属于$notStatuses的视频。

2 个答案:

答案 0 :(得分:1)

您必须groupOR子句:

$videos = $videos->where(function($query) use($statuses) {
    $query->whereJsonContains('video_status_ids', $statuses[0])
        ->orWhereJsonContains('video_status_ids', $statuses[1])
        ->orWhereJsonContains('video_status_ids', $statuses[2])
        ->orWhereJsonContains('video_status_ids', $statuses[3])
        ->orWhereJsonContains('video_status_ids', $statuses[4])
        ->orWhereJsonContains('video_status_ids', $statuses[5])
        ->orWhereJsonContains('video_status_ids', $statuses[6])
        ->orWhereJsonContains('video_status_ids', $statuses[7])
        ->orWhereJsonContains('video_status_ids', $statuses[8])
        ->orWhereJsonContains('video_status_ids', $statuses[9]);
});

或使用foreach

 $videos = $videos->where(function($query) use($statuses) {
     foreach($statuses as $status) {
        $query->orWhereJsonContains('video_status_ids', $status); 
    }
 }

答案 1 :(得分:1)

尝试这样。

$statuses = array();

$videos = DB::table('videos')
                ->where('user_id', $user_id)
                ->where(function ($query) use($statuses, $user_id) {
                  for($i = 0; $i <= 9; $i++){
                    $query->where('user_id', $user_id)
                     ->WhereJsonContains('video_status_ids', $statuses[$i]);
                    $queryResult[$i] = $query;
                  }
                });


$videos = $videos->orderBy('video_priority', 'desc')->paginate(10);

return $videos;

但是如果以后需要按用户进行查询,则可以使用我的代码。:)

编码愉快!!! :)