Laravel Query使用AND过滤不同的表

时间:2015-09-12 16:38:29

标签: php mysql laravel

我的Larvel Query出了问题,它根本无法正常工作。 它应该使用“开始”和“结束”属性“团队”,“频道”,“区域”和“时间段”过滤“广告系列”。时间段没问题。 用户可以过滤不同的团队/频道/区域,并且可以选择否,一个或多个属性,它们应该作为或连接。 所需的结果应该为所有具有所选团队和频道AND区域的Campaign提供。但目前我得到的每个广告系列都有选定的小组或选定的频道或所选区域。

希望你能有人帮助我。

   public function searchCampaigns($page, $limit, $data)
    {
        $startDate = $data['startDate'];
        $endDate = $data['endDate'];

        $campaigns = Campaign::distinct()->select('campaigns.*')
                    ->join('campaign_team', 'campaign_team.campaign_id', '=', 'campaigns.id')
                    ->join('campaign_region', 'campaign_region.campaign_id', '=', 'campaigns.id')
                    ->join('campaign_channel', 'campaign_channel.campaign_id', '=', 'campaigns.id')
                    ->join('teams', 'campaign_team.team_id', '=', 'teams.id')
                    ->join('regions', 'campaign_region.region_id', '=', 'regions.id')
                    ->join('channels', 'campaign_channel.channel_id', '=', 'channels.id')
//Interesting Part
                    ->whereIn('teams.id', $data['teams'])
                    ->whereIn('regions.id', $data['regions'])
                    ->whereIn('channels.id', $data['channels'])
                    ->where(function ($query) use ($startDate, $endDate) {
                        $query->where('campaigns.start_date', '>=', $startDate)
                            ->where('campaigns.end_date', '<=', $endDate);
                    })

                    ->orWhere(function ($query) use ($startDate, $endDate) {
                        $query->where('campaigns.end_date', '<=', $endDate)
                            ->where('campaigns.end_date', '>=', $startDate);
                    })

                    ->orWhere(function ($query) use ($startDate, $endDate) {
                        $query->where('campaigns.start_date', '>=', $startDate)
                            ->where('campaigns.start_date', '<=', $endDate);
                    })

                    ->orWhere(function ($query) use ($startDate, $endDate) {
                        $query->where('campaigns.start_date', '<=', $startDate)
                            ->Where('campaigns.end_date', '>=', $endDate);
                    })                    ->orderBy('start_date', 'asc');

        $result['count'] = sizeof($campaigns->lists('id'));
        $result['campaigns'] = $campaigns->skip($limit * ($page - 1))->take($limit)->get();

        return $result;
    }

1 个答案:

答案 0 :(得分:2)

试试这个。它应该工作。如果发生任何问题,请告诉我:))

public function searchCampaigns($page, $limit, $data)
{
    $result = [];     

    // Checking Validity of Start Date
    if (empty($data['startDate']) || (Carbon::createFromFormat('Y-m-d', $data['startDate']) === false))
        return $result;

    // Checking Validity of End Date                
    if (empty($data['endDate']) || (Carbon::createFromFormat('Y-m-d', $data['endDate']) === false))
        return $result;                 

    // Comparing Start & End Date
    $startDate = Carbon::parse($data['startDate']);
    $endDate = Carbon::parse($data['endDate']);

    if($startDate->gt($endDate))
        return $result;

    if($endDate->lt($startDate))
        return $result;

    $startDate = $data['startDate'];
    $endDate = $data['endDate'];                        

    $campaigns = Campaign::distinct()->select('campaigns.*')
                ->join('campaign_team', 'campaign_team.campaign_id', '=', 'campaigns.id')
                ->join('campaign_region', 'campaign_region.campaign_id', '=', 'campaigns.id')
                ->join('campaign_channel', 'campaign_channel.campaign_id', '=', 'campaigns.id')
                ->join('teams', 'campaign_team.team_id', '=', 'teams.id')
                ->join('regions', 'campaign_region.region_id', '=', 'regions.id')
                ->join('channels', 'campaign_channel.channel_id', '=', 'channels.id')
                ->whereIn('teams.id', $data['teams'])
                ->whereIn('regions.id', $data['regions'])
                ->whereIn('channels.id', $data['channels'])
                ->where('campaigns.start_date', '>=', $startDate)
                ->where('campaigns.end_date', '<=', $endDate);
                ->orderBy('start_date', 'asc');

    $result['count'] = sizeof($campaigns->lists('id'));
    $result['campaigns'] = $campaigns->skip($limit * ($page - 1))->take($limit)->get();

    return $result;
}