1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Telefonica\Repositories; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Schema; |
6
|
|
|
use Telefonica\Models\Actors\Person; |
7
|
|
|
|
8
|
|
|
class PersonRepository |
9
|
|
|
{ |
10
|
|
|
public function __construct(Person $model) |
11
|
|
|
{ |
12
|
|
|
$this->model = $model; |
|
|
|
|
13
|
|
|
} |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Returns all Persons. |
17
|
|
|
* |
18
|
|
|
* @return \Illuminate\Database\Eloquent\Collection|static[] |
19
|
|
|
*/ |
20
|
|
|
public function all() |
21
|
|
|
{ |
22
|
|
|
return $this->model->all(); |
23
|
|
|
// return $this->model->orderBy('created_at', 'desc')->all(); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Returns all paginated Persons. |
28
|
|
|
* |
29
|
|
|
* @return \Illuminate\Database\Eloquent\Collection|static[] |
30
|
|
|
*/ |
31
|
|
|
public function paginated() |
32
|
|
|
{ |
33
|
|
|
if (isset(request()->dir) && isset(request()->field)) { |
34
|
|
|
$model = $this->model->orderBy(request()->field, request()->dir); |
35
|
|
|
} else { |
36
|
|
|
$model = $this->model->orderBy('created_at', 'desc'); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
return $model->paginate(\Illuminate\Support\Facades\Config::get('cms.pagination', 25)); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Searches the orders. |
44
|
|
|
* |
45
|
|
|
* @return \Illuminate\Database\Eloquent\Collection|static[] |
46
|
|
|
*/ |
47
|
|
|
public function search($payload, $count) |
48
|
|
|
{ |
49
|
|
|
$query = $this->model->orderBy('created_at', 'desc'); |
50
|
|
|
|
51
|
|
|
$columns = Schema::getColumnListing('orders'); |
52
|
|
|
$query->where('id', '>', 0); |
53
|
|
|
$query->where('id', 'LIKE', '%'.$payload.'%'); |
54
|
|
|
|
55
|
|
|
foreach ($columns as $attribute) { |
56
|
|
|
$query->orWhere($attribute, 'LIKE', '%'.$payload.'%'); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return [$query, $payload, $query->paginate($count)->render()]; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Stores Persons into database. |
64
|
|
|
* |
65
|
|
|
* @param array $payload |
66
|
|
|
* |
67
|
|
|
* @return Persons |
68
|
|
|
*/ |
69
|
|
|
public function store($payload) |
70
|
|
|
{ |
71
|
|
|
return $this->model->create($payload); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Find Persons by given id. |
76
|
|
|
* |
77
|
|
|
* @param int $id |
78
|
|
|
* |
79
|
|
|
* @return \Illuminate\Support\Collection|null|static|Persons |
80
|
|
|
*/ |
81
|
|
|
public function find($id) |
82
|
|
|
{ |
83
|
|
|
return $this->model->find($id); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Find Persons by given id. |
88
|
|
|
* |
89
|
|
|
* @param int $id |
90
|
|
|
* |
91
|
|
|
* @return \Illuminate\Support\Collection|null|static|Persons |
92
|
|
|
*/ |
93
|
|
|
public function getByCustomer($id) |
94
|
|
|
{ |
95
|
|
|
return $this->model->where('user_id', '=', $id); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Find Persons by given id. |
100
|
|
|
* |
101
|
|
|
* @param int $id |
102
|
|
|
* |
103
|
|
|
* @return \Illuminate\Support\Collection|null|static|Persons |
104
|
|
|
*/ |
105
|
|
|
public function getByCustomerAndId($customer, $id) |
106
|
|
|
{ |
107
|
|
|
return $this->model->where('user_id', $customer)->where('id', $id)->first(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Find Persons by given id. |
112
|
|
|
* |
113
|
|
|
* @param int $id |
114
|
|
|
* |
115
|
|
|
* @return \Illuminate\Support\Collection|null|static|Persons |
116
|
|
|
*/ |
117
|
|
|
public function getByCustomerAndUuid($customer, $id) |
118
|
|
|
{ |
119
|
|
|
return $this->model->where('user_id', $customer)->where('uuid', $id)->first(); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Updates Persons into database. |
124
|
|
|
* |
125
|
|
|
* @param Person $order |
126
|
|
|
* @param array $payload |
127
|
|
|
* |
128
|
|
|
* @return Persons |
129
|
|
|
*/ |
130
|
|
|
public function update($order, $payload) |
131
|
|
|
{ |
132
|
|
|
if (isset($payload['is_shipped'])) { |
133
|
|
|
$payload['is_shipped'] = true; |
134
|
|
|
} else { |
135
|
|
|
$payload['is_shipped'] = false; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return $order->update($payload); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: