递减值

时间:2019-04-07 07:34:38

标签: php laravel

我正在尝试建立一个查询模型,在该模型中,每次发布查询时,都会通过邮件通知特定用户,并且将其记录减少1。 邮件工作正常,但是递减的发生是由于所标识的用户数而不是1。

尝试过各种财产险。

$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');
        });
    }
}

每次运行代码时,应将user_plans减1,而不是发送的电子邮件数。

感谢大家的帮助。

1 个答案:

答案 0 :(得分:0)

这是无法正常工作的零件,请尝试以下操作:

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();