zoukankan      html  css  js  c++  java
  • laravel 容器注入的坑

    今天遍历添加数据时遇到个坑,哪位大神知道什么原因??

    起初的代码是这样的:(部分)

    public function addActive(Request $request, Activenorms $activenorms){
    $specifications = $request->get('specification');     //活动规格
    
    if(!$specifications || !is_array($specifications)){
       return response()->json(['message' => '请填写正确活动规格'], 500);
    }
    
      foreach ($specifications as $key=>$value) {
         $activeNorms->active_id=$id;
         $activeNorms->active_price_name=$value['active_price_name'];
         $activeNorms->active_price_money=$value['active_price_money'];
         $activeNorms->active_max_players=$value['active_max_players'];
         $activeNorms->save();
      }
    
    }

    这样的代码是无论数组中有多少规格,永远只在数据表里添加了最后一条数据...

    试着改成这样:
    public function addActive(Request $request){
    $specifications = $request->get('specification');     //活动规格
    
    if(!$specifications || !is_array($specifications)){
       return response()->json(['message' => '请填写正确活动规格'], 500);
    }
    
      foreach ($specifications as $key=>$value) {
         $activeNorms=new ActiveNorms();
         $activeNorms->active_id=$id;
         $activeNorms->active_price_name=$value['active_price_name'];
         $activeNorms->active_price_money=$value['active_price_money'];
         $activeNorms->active_max_players=$value['active_max_players'];
         $activeNorms->save();
      }
    
    }

    然后就能正常的添加多条数据了...

    什么原因???有大神能解析下吗??


  • 相关阅读:
    USACO Section 1.4 Mother's Milk
    USACO Section 1.5 Checker Challenge
    USACO Section 1.5 Number Triangles
    九度 1140 八皇后
    九度 1091 棋盘游戏
    USACO Section 2.1 Sorting A ThreeValued Sequence
    USACO Section 1.4 The Clocks
    USACO Section 1.5 Superprime Rib
    USACO Section 2.1 Ordered Fractions
    双目测距与三维重建
  • 原文地址:https://www.cnblogs.com/sgm4231/p/9641478.html
Copyright © 2011-2022 走看看