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();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程