1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use App\Models\Addr; |
6
|
|
|
use Illuminate\Http\Request; |
7
|
|
|
|
8
|
|
|
class AddrController extends Controller |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Display a listing of the resource. |
12
|
|
|
* |
13
|
|
|
* @return \Illuminate\Http\Response |
14
|
|
|
*/ |
15
|
|
|
public function index(Request $request) |
16
|
|
|
{ |
17
|
|
|
$query = Addr::query(); |
18
|
|
|
|
19
|
|
|
if ($request->has('searchTerm')) { |
20
|
|
|
$columnsToSearch = ['adr1', 'adr2', 'city', 'stae', 'post', 'ctry']; |
21
|
|
|
$search_term = json_decode($request->searchTerm)->searchTerm; |
22
|
|
|
if (! empty($search_term)) { |
23
|
|
|
$searchQuery = '%'.$search_term.'%'; |
24
|
|
|
foreach ($columnsToSearch as $column) { |
25
|
|
|
$query->orWhere($column, 'LIKE', $searchQuery); |
26
|
|
|
} |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
if ($request->has('columnFilters')) { |
31
|
|
|
$filters = get_object_vars(json_decode($request->columnFilters)); |
32
|
|
|
|
33
|
|
|
foreach ($filters as $key => $value) { |
34
|
|
|
if (! empty($value)) { |
35
|
|
|
$query->orWhere($key, 'like', '%'.$value.'%'); |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
if ($request->has('sort.0')) { |
41
|
|
|
$sort = json_decode($request->sort[0]); |
42
|
|
|
$query->orderBy($sort->field, $sort->type); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
if ($request->has('perPage')) { |
46
|
|
|
$rows = $query->paginate($request->perPage); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return $rows; |
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Show the form for creating a new resource. |
54
|
|
|
* |
55
|
|
|
* @return \Illuminate\Http\Response |
56
|
|
|
*/ |
57
|
|
|
public function create() |
58
|
|
|
{ |
59
|
|
|
$address = Addr::get(); |
60
|
|
|
|
61
|
|
|
return $address; |
|
|
|
|
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Store a newly created resource in storage. |
66
|
|
|
* |
67
|
|
|
* @param \Illuminate\Http\Request $request |
68
|
|
|
* @return \Illuminate\Http\Response |
69
|
|
|
*/ |
70
|
|
|
public function store(Request $request) |
71
|
|
|
{ |
72
|
|
|
$request->validate([ |
73
|
|
|
|
74
|
|
|
'city' => 'required', |
75
|
|
|
'stae' => 'required', |
76
|
|
|
|
77
|
|
|
]); |
78
|
|
|
|
79
|
|
|
return Addr::create([ |
|
|
|
|
80
|
|
|
'adr1' => $request->adr1, |
81
|
|
|
'adr2' => $request->adr2, |
82
|
|
|
'city' => $request->city, |
83
|
|
|
'stae' => $request->stae, |
84
|
|
|
'post' => $request->post, |
85
|
|
|
'ctry' => $request->ctry, |
86
|
|
|
]); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Display the specified resource. |
91
|
|
|
* |
92
|
|
|
* @param int $id |
93
|
|
|
* @return \Illuminate\Http\Response |
94
|
|
|
*/ |
95
|
|
|
public function show($id) |
96
|
|
|
{ |
97
|
|
|
return Addr::find($id); |
|
|
|
|
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Show the form for editing the specified resource. |
102
|
|
|
* |
103
|
|
|
* @param int $id |
104
|
|
|
* @return \Illuminate\Http\Response |
105
|
|
|
*/ |
106
|
|
|
public function edit($id) |
|
|
|
|
107
|
|
|
{ |
108
|
|
|
// |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Update the specified resource in storage. |
113
|
|
|
* |
114
|
|
|
* @param \Illuminate\Http\Request $request |
115
|
|
|
* @param int $id |
116
|
|
|
* @return \Illuminate\Http\Response |
117
|
|
|
*/ |
118
|
|
|
public function update(Request $request, $id) |
119
|
|
|
{ |
120
|
|
|
$request->validate([ |
121
|
|
|
'city' => 'required', |
122
|
|
|
'stae' => 'required', |
123
|
|
|
]); |
124
|
|
|
|
125
|
|
|
$addr = Addr::find($id); |
126
|
|
|
$addr->adr1 = $request->adr1; |
127
|
|
|
$addr->adr2 = $request->adr2; |
128
|
|
|
$addr->city = $request->city; |
129
|
|
|
$addr->stae = $request->stae; |
130
|
|
|
$addr->post = $request->post; |
131
|
|
|
$addr->ctry = $request->ctry; |
132
|
|
|
$addr->save(); |
133
|
|
|
|
134
|
|
|
return $addr; |
|
|
|
|
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Remove the specified resource from storage. |
139
|
|
|
* |
140
|
|
|
* @param int $id |
141
|
|
|
* @return \Illuminate\Http\Response |
142
|
|
|
*/ |
143
|
|
|
public function destroy($id) |
144
|
|
|
{ |
145
|
|
|
$addr = Addr::find($id); |
146
|
|
|
if ($addr) { |
147
|
|
|
$addr->delete(); |
148
|
|
|
|
149
|
|
|
return 'true'; |
|
|
|
|
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return 'false'; |
|
|
|
|
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
public function get() |
156
|
|
|
{ |
157
|
|
|
$address = Addr::all(); |
158
|
|
|
|
159
|
|
|
return $address; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|