-
Notifications
You must be signed in to change notification settings - Fork 0
/
CO2DataController.php
49 lines (43 loc) · 2.15 KB
/
CO2DataController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
namespace App\Http\Controllers;
use App\Models\CO2Data;
use Illuminate\Http\Request;
class CO2DataController extends Controller
{
public function index() {
return CO2Data::all();
}
public function show(CO2Data $data) {
return $data;
}
public function store(Request $request) {
// validate
$data = $request->validate([
'msg' => 'sometimes|string',
'code' => 'sometimes|integer',
'co2total' => 'required|numeric',
'co2params.*' => 'required|array',
'co2params.*.value' => 'sometimes|required|numeric', // only sometimes, because it is not present in sector6
'co2params.*.params' => 'sometimes|required|array', // only sometimes, because it is not present in sector6
'co2params.sector1.params.size' => 'required|integer',
'co2params.sector1.params.renew' => 'required|boolean',
'co2params.sector1.params.eco' => 'required|boolean',
'co2params.sector2.params.nocar' => 'required|boolean',
'co2params.sector2.params.freqfly' => 'required|boolean',
'co2params.sector3.params.nomeat' => 'required|boolean',
'co2params.sector3.params.muchmeat' => 'required|boolean',
'co2params.sector4.params.nomoney' => 'required|boolean',
'co2params.sector4.params.muchmoney' => 'required|boolean',
'co2params.sector4.params.stock' => 'required|boolean',
'location' => 'required|array',
'location.district' => 'required|integer',
'location.mult' => 'required|integer'
]);
// save to database
$model = new CO2Data;
$model->co2total = $data['co2total'];
$model->co2params = json_encode($data['co2params']);
$model->location = json_encode($data['location']);
$model->save();
}
}