示例#1
0
 /**
  * @return Builder
  */
 public function sensors()
 {
     return Sensor::join('products', 'sensors.product_id', '=', 'products.id')->join('locations', 'locations.product_id', '=', 'products.id')->where('locations.user_id', $this->getKey());
 }
 /**
  * Store a newly created resource in storage.
  *
  * @param  \Illuminate\Http\Request  $request
  * @return \Illuminate\Http\Response
  */
 public function store(CreateSamplingsRequest $request)
 {
     $samplings_groupBy_gsIds = collect($request->samplings)->groupBy('generic_sensor_id');
     /**
      * for querying the sensors, we remove any object that didn't have a "generic_sensor_id" field
      *
      * Parameters:
      * "samplings": [
      *     {
      *         "generic_sensor_id": 2,
      *         "value": 49.72
      *     },
      *     {
      *         "generic_sensor_id": 3,
      *         "value": 104.693
      *     },
      *     {
      *         "generic_sensor_id": 4,
      *         "value": 22.1
      *     },
      *     {
      *         "generic": 1
      *     }
      * ]
      *
      *
      * After group:
      * {
      *     "2": [
      *         {
      *             "generic_sensor_id": 2,
      *             "value": 49.72
      *         }
      *     ],
      *     "3": [
      *         {
      *             "generic_sensor_id": 3,
      *             "value": 104.693
      *         }
      *     ],
      *     "4": [
      *         {
      *             "generic_sensor_id": 4,
      *             "value": 22.1
      *         }
      *     ],
      *     "": [
      *         {
      *             "generic": 1
      *         }
      *     ]
      * }
      */
     $samplings_groupBy_gsIds->forget("");
     $sensors = Sensor::join('generic_sensors', 'sensors.generic_sensor_id', '=', 'generic_sensors.id')->join('products', 'sensors.product_id', '=', 'products.id')->whereIn('generic_sensors.id', $samplings_groupBy_gsIds->keys())->where('products.id', $request->product_id)->get(['sensors.id', 'sensors.generic_sensor_id']);
     $sensor_for_gsId = array();
     foreach ($sensors as $sensor) {
         $sensor_for_gsId[$sensor->generic_sensor_id] = $sensor->getKey();
     }
     $newSamplings = array();
     $today = Carbon::now();
     foreach ($request->samplings as $sampling) {
         if (!array_has($sampling, 'generic_sensor_id') || !array_has($sampling, 'value')) {
             continue;
         }
         $created_at = $today;
         if (array_has($sampling, 'created_at') && strtotime($sampling['created_at'])) {
             $created_at = Carbon::createFromTimestamp(strtotime($sampling['created_at']));
         }
         array_push($newSamplings, array('sensor_id' => $sensor_for_gsId[$sampling['generic_sensor_id']], 'sampled' => $sampling['value'], 'created_at' => $created_at));
     }
     $successfully_inserted = DB::table('samplings')->insert($newSamplings);
     return $successfully_inserted ? 'Created ' . count($newSamplings) . ' sampling(s)' : 'Error during inserting';
 }