source: app/Http/Controllers/OfficerController.php@ 3c89e27

main
Last change on this file since 3c89e27 was 3c89e27, checked in by bube-ristovska <ristovska725@…>, 4 months ago

Refactored duplicate code

  • Property mode set to 100644
File size: 3.2 KB
Line 
1<?php
2
3namespace App\Http\Controllers;
4
5use App\Models\Policeman;
6use Illuminate\Contracts\Auth\Authenticatable;
7use Illuminate\Http\Request;
8use Illuminate\Support\Carbon;
9use Illuminate\Support\Facades\DB;
10use Illuminate\Support\Facades\Session;
11
12class OfficerController extends Controller
13{
14 private function policeStationIsPoliceman()
15 {
16 return DB::table('police_station')
17 ->where('p_id', Session::get('p_id'))
18 ->get();
19 }
20 private function policeStationIsOfficer()
21 {
22 return DB::table('police_station')
23 ->where('pe_id', Session::get('pe_id'))
24 ->get();
25 }
26 function employees()
27 {
28 if(Session::get('pe_id') == null) {
29 return view('login');
30 }
31 if(Session::get('is_policeman')){
32 $police_station = $this->policeStationIsPoliceman();
33 } else {
34 $police_station = $this->policeStationIsOfficer();
35 }
36 $results = DB::table('policeman')
37 ->join('people', 'policeman.pe_id', '=', 'people.pe_id')
38 ->where('policeman.p_id', $police_station[0]->p_id)
39 ->get();
40
41
42 return view('employees', [
43 'employees' => $results,
44 'p_address'=>$police_station[0]->p_address
45 ]);
46 }
47
48 function show($id){
49 if(Session::get('is_policeman')){
50 $police_station = $this->policeStationIsPoliceman();
51 } else {
52 $police_station = $this->policeStationIsOfficer();
53 }
54 $result = DB::table('policeman')
55 ->join('people', 'policeman.pe_id', '=', 'people.pe_id')
56 ->where('p_id', $police_station[0]->p_id)
57 ->where('people.pe_id', $id)
58 ->get();
59 $cases = DB::table('statements')
60 ->where('pe_id', $id)
61 ->get();
62 return view('employee', [
63 'employee' => $result[0],
64 'p_address'=>$police_station[0]->p_address,
65 'cases' => $cases
66 ]);
67 }
68
69 function register()
70 {
71 return view('register-policeman');
72 }
73 function register_post()
74 {
75 $policeman = request()->validate([
76 'badge_no' => 'required',
77 'embg' => 'required',
78 'password' => 'required',
79 'rank'=>'required'
80 ]);
81
82
83 $police_station = $this->policeStationIsOfficer();
84 $pe_id = DB::table('people')
85 ->where('embg', $policeman['embg'])->get();
86 $data = [
87 'pe_id' => $pe_id[0]->pe_id,
88 'badge_no' => $policeman["badge_no"],
89 'p_date_of_employment' => Carbon::now()->format('Y-m-d'),
90 'rank' => $policeman["rank"],
91 'p_id' => $police_station[0]->p_id,
92 'p_password' => $policeman["password"]
93 ];
94 DB::table('policeman')->insert($data);
95// DB::insert('INSERT INTO policeman (pe_id, badge_no, p_date_of_employment, rank, p_id, p_password) VALUES (?, ?, ?, ?, ?, ?)', [$pe_id[0]->pe_id, $policeman["badge_no"], Carbon::now()->format('Y-m-d'), $policeman["rank"], $police_station[0]->p_id,$policeman["password"]]);
96 return redirect()->back()->with('message',"Додадено");
97 }
98
99
100
101
102}
Note: See TracBrowser for help on using the repository browser.