保存数据时,将随机值添加到表行 - mysql / laravel

时间:2015-10-13 08:29:22

标签: php mysql laravel-5.1

我有一张表,在那里我想保存检查清单值,其中12种类型具有相关的值。

例如:

键入:1值:a

输入:2值:c

键入:3值:c

同样有12种类型,所以我想手动添加1到12个值。如何进行循环?

1 个答案:

答案 0 :(得分:0)

    //the code below is a string so explode it
    $pre_departure_type = $request->pre_departure_type;

   //exploded to array
    $arrayPreDepartureType = explode(',', $pre_departure_type);

    for($i = 0; i < count($arrayPreDepartureType); $i++){
     $DriverPreDepartureItems = new DriverPreDepartureItems();
     $DriverPreDepartureItems->user_id = $driver_id->id;

     $DriverPreDepartureItems->shift_id = $find_shift->id;
     // there are 10 departure types //
     $DriverPreDepartureItems->pre_departure_type = $arrayPreDepartureType[i];

     $DriverPreDepartureItems->value = $value;

     // there are 10 departure types //
     $DriverPreDepartureItems->save();
    }
    //It will save until it reaches the maximum count.