php - Decrement value in a loop

473

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('[email protected]')
                ->subject('Urgent: This lead does not have any takers');
        });
    }
}

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

Thanks guys for the help.

860

Answer

Solution:

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

People are also looking for solutions to the problem: php - Laravel delete method not allowed

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.