发出不止一次调用的laravel保存函数

时间:2017-08-26 07:58:48

标签: php laravel

我的laravel控制器中的功能一次被调用3次

当我更改返回重定向或json响应时,它可以正常工作 但我必须使用$coupon_id返回视图 它工作得很好,突然发生了这个问题

那是我的代码

                $coupon['user_id'] = $user->id;
                $coupon['part_count']  = count($parts);
                $coupon['type']  = $coupon_type;
                $coupon['created_at']= \Carbon\Carbon::now()->addHour(2)->toDateTimeString();
                $coupon['updated_at']= \Carbon\Carbon::now()->addHour(2)->toDateTimeString();
                $coupon_id = DB::table('coupons')->insertGetId($coupon);


                if($coupon_type == 'system'){
                    $sys = $results['sys_list'];
                    foreach ($sys as $s){
                        $s->coupon_id = $coupon_id;
                        $s->save();
                    }
                }

                foreach($parts as $ap) {
                    $existing_part = Part::where("code", "=", $ap->code)->first();
                    if($existing_part != null){
                        if($existing_part->part_one_name == $ap->part_one_name){
                            $part_id = $existing_part->id;
                        }else{
                            $existing_part->old_code = $existing_part->code;
                            $existing_part->code = null;
                            $existing_part->save();
                            $part['code'] = $ap->code;
                            $part['league'] = $ap->league;
                            $part['part_one_name'] = $ap->part_one_name;
                            $part['part_two_name'] = $ap->part_two_name;
                            $part['created_at']= \Carbon\Carbon::now()->toDateTimeString();
                            $part['updated_at']= \Carbon\Carbon::now()->toDateTimeString();
                            $part_id = DB::table('parts')->insertGetId($part);
                        }

                    }else{

                        $part=array(

                        'code' => $ap['code'],
                        'league' => $ap['league'],
                        'part_one_name' => $ap['part_one_name'],
                        'part_two_name' => $ap['part_two_name'],
                        'created_at'=> \Carbon\Carbon::now()->toDateTimeString(),
                        'updated_at'=> \Carbon\Carbon::now()->toDateTimeString(),
                        );

                        $part_id = DB::table('parts')->insertGetId($part);
                    }
                         DB::table('part_coupon')->insert(
                        [
                            'part_id' => $part_id,
                            'coupon_id' => $coupon_id,
                            'type' => $ap->type,
                            'cote'=> $ap->cote,
                            'fixe'=> $ap->fixe,
                            'created_at'=> \Carbon\Carbon::now()->toDateTimeString(),
                            'updated_at'=> \Carbon\Carbon::now()->toDateTimeString()
                        ]
                    );
                }
                $user->solde = $user->solde - $this->stringToFloat($pr);

                $user->save();

                return \view('home._ok', compact('coupon_id'));

                }

                else{

                    return "invalid amount";
                }

就是路线

Route::get('/callMeDirectlyFromUrl', 'HomeController@callMeDirectlyFromUrl');

这就是网络开发工具正在展示的内容

issue

1 个答案:

答案 0 :(得分:0)

而不是

return \view('home._ok', compact('coupon_id'));

使用渲染方法

return view('home._ok',compact('coupon_id'))->render();