-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVehicleController.php
111 lines (90 loc) · 2.63 KB
/
VehicleController.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
namespace App\Http\Controllers;
use App\Http\Requests\RegisterRequest;
use App\Models\AuthorizedCode;
use App\Models\Brand;
use App\Models\Person;
use App\Models\Vehicle;
use Exception;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Support\Facades\DB;
class VehicleController extends Controller
{
public function index($id = null)
{
$authenticated = request()
->session()
->get('authenticated', false);
if (!$authenticated && $id == null) {
return redirect('forbidden');
}
if (!$authenticated) {
try {
AuthorizedCode::query()
->where('code', '=', $id)
->firstOrFail();
request()
->session()
->put('authenticated', true);
} catch (ModelNotFoundException $e) {
return redirect('forbidden');
}
}
return view('home');
}
public function forbidden()
{
return view('forbidden');
}
public function register()
{
$brands = Brand::all();
return view('register', [
'brands' => $brands,
]);
}
public function store(RegisterRequest $request)
{
$validatedData = $request->validated();
$validatedData['plate'] = strtoupper($validatedData['plate']);
$person = new Person();
$person->fill($validatedData);
$vehicle = new Vehicle();
$vehicle->fill($validatedData);
DB::beginTransaction();
try {
$person->save();
$person
->vehicle()
->save($vehicle);
} catch (Exception $e) {
DB::rollBack();
return back()
->withInput()
->with([
'message' => 'Ocurrió un error, intenta más tarde',
'success' => false,
]);
}
DB::commit();
return back()->with([
'message' => 'El vehiculo se ha registrado correctamente',
'success' => true,
]);
}
public function vehicles()
{
$vehicles = Vehicle::with('people', 'brand')
->get();
return view('vehicles', [
'vehicles' => $vehicles,
]);
}
public function stats()
{
$stats = DB::select('SELECT count(plate) AS amount, brands.name as brand FROM vehicles INNER JOIN brands on vehicles.brand_id = brands.id GROUP BY brands.id');
return view('stats', [
'stats' => $stats
]);
}
}