如何在db laravel中保存多个数组

时间:2015-10-14 08:57:20

标签: php laravel-5.1

我从服务中获取这些数据

值= 1:1,2:0,3:1,4:0,5:1,6:1,7:0,8:0,9:1,10:0

我使用arrayas将其拆分

$result = array();
                foreach (explode(',',$value) as $sub)
                {
                    $subAry = explode(':',$sub);
                    $result[$subAry[0]] = $subAry[1];

                }
                return $result;

here how its return
  "1": "1",
  "2": "0",
  "3": "1",
  "4": "0",
  "5": "1",
  "6": "1",
  "7": "0",
  "8": "0",
  "9": "1",
  "10": "0"
}

我想将这些值保存在两列的表格中,请提供建议

第一个数字集应该转到$ value列,另一个应该转到$ type列,

这是我的控制器,

$DriverPreDepartureItems = new DriverPreDepartureItems();
                    $DriverPreDepartureItems->user_id = $driver_id->id;

                    $DriverPreDepartureItems->shift_id = $find_shift->id;
                    $DriverPreDepartureItems->type = ;
                    $DriverPreDepartureItems->value = ;

                    $DriverPreDepartureItems->save();

请咨询

1 个答案:

答案 0 :(得分:0)

既然你有这样的结果, 您仍然可以使用foreach循环访问它们并将它们保存到您的表中 就像下面一样

foreach ($result as $key => $value) {
    /* do your saving codes here
    * the values of $key are (1,2,3,4,..,10)
    * and $values will be (1 or 0) */

    $DriverPreDepartureItems = new DriverPreDepartureItems();
    $DriverPreDepartureItems->user_id = $driver_id->id;

    $DriverPreDepartureItems->shift_id = $find_shift->id;
    $DriverPreDepartureItems->type = $value;
    $DriverPreDepartureItems->value = $key;

    $DriverPreDepartureItems->save();

 }