dongshen4129 2019-04-07 07:34
浏览 55
已采纳

减少循环中的值

I am trying to make a query model where every time a query is posted, specific users are notified by mail and their records are decremented by 1. Mail is working fine but the decrement is happening by the number of users identified and not 1.

Have tried various P&C.

$exclusive = DB::table('user_plans')
    ->leftJoin('companies', 'companies.id' , '=', 'user_plans.user_id')
    ->select('user_plans.*')
    ->where('user_plans.service_id' , '=', $input['project'])
    ->where('user_plans.lead_type' , '=', 2)
    ->where('user_plans.count' , '>', 0)
    ->limit(5)
    ->get();

$shared = DB::table('user_plans')
    ->leftJoin('companies', 'companies.id' , '=', 'user_plans.user_id')
    ->select('user_plans.*')
    ->where('user_plans.service_id' , '=', $input['project'])
    ->where('user_plans.lead_type' , '=', 1)
    ->where('user_plans.count' , '>', 0)
    ->limit(500)
    ->get();

$project = DB::table('services')
    ->select('services.*')
    ->where('services.id' , '=', $input['project'])
    ->get();

if (count($exclusive) > 0) {
    foreach ($exclusive as $exclusive) {
        Mail::send('emails.exclusive', array(
            'name'    => $input['name'],
            'email'   => $input['email'],
            'phone'   => $input['phone'],
            'detail'  => $input['detail'],
            'project' => $project[0]->name,
            'budget'  => $input['budget']
        ), function($message) use ($exclusive) {
            $message
                ->to($exclusive->lead_email)
                ->subject('CXO Forest Contact Us!');
        });

        //This part is not working
        $deduct =  DB::table('user_plans')
            ->where('user_plans.user_id' , '=', $exclusive->user_id)
            ->where('user_plans.count' , '>', 0)
            ->where('user_plans.type' , '=', 2)                 
            //->get();
            ->decrement('count', 1);
    }
} else {
    if (count($shared) > 0) {           
        foreach ($shared as $shared) {
            Mail::send('emails.exclusive', array(
                'name'    => $input['name'],
                'email'   => $input['email'],
                'phone'   => $input['phone'],
                'detail'  => $input['detail'],
                'project' => $project[0]->name,
                'budget'  => $input['budget']
            ), function($message) use ($shared) {
                $message
                    ->to($shared->lead_email)
                    ->subject('CXO Forest Contact Us!');
            });

            $deductx = DB::table('user_plans')
                ->where('user_plans.user_id' , '=', $shared->user_id)
                ->where('user_plans.count' , '>', 0)
                ->where('user_plans.type' , '=', 2)
                ->decrement('count', 1);
        }
    } else {
        Mail::send('emails.exclusive', array(
            'name'    => $input['name'],
            'email'   => $input['email'],
            'phone'   => $input['phone'],
            'detail'  => $input['detail'],
            'project' => $project[0]->name,
            'budget'  => $input['budget']
        ), function($message) use ($shared) {
            $message
                ->to('amit.khare@studyspectrum.com')
                ->subject('Urgent: This lead does not have any takers');
        });
    }
}

Every time the code runs, deduct should reduce user_plans by 1 and not the number of emails sent.

Thanks guys for the help.

  • 写回答

1条回答 默认 最新

  • dozpox8752 2019-04-07 13:55
    关注

    Here's the part part that is not working, try this:

    DB::table('user_plans') ->where('user_plans.user_id' , '=', $exclusive->user_id) ->where('user_plans.count' , '>', 0) ->where('user_plans.type' , '=', 2)  ->take(1)->delete();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?