2
2
3
3
namespace Mawuekom \Repository \Controls ;
4
4
5
+ use Illuminate \Database \Eloquent \Builder ;
6
+
5
7
trait RepositoryQuery
6
8
{
9
+ /**
10
+ * Retrieve all data of repository
11
+ *
12
+ * @param array $columns
13
+ *
14
+ * @return mixed
15
+ */
16
+ public function all ($ columns = ['* ' ])
17
+ {
18
+ $ this ->applyCriteria ();
19
+ $ this ->applyScope ();
20
+
21
+ $ results = ($ this ->model instanceof Builder)
22
+ ? $ this ->model ->get ($ columns )
23
+ : $ this ->model ->all ($ columns );
24
+
25
+ $ this ->resetModel ();
26
+ $ this ->resetScope ();
27
+
28
+ return $ results ;
29
+ }
30
+
31
+ /**
32
+ * Retrieve the list of data and can add some adjustments to it
33
+ * Like model's relations...
34
+ *
35
+ * @param string $orderByColumn
36
+ * @param string $orderBy
37
+ * @param array $with
38
+ * @param array $columns
39
+ *
40
+ * @return mixed
41
+ */
42
+ public function list ($ orderByColumn , $ orderBy = 'desc ' , $ with = [], $ columns = ['* ' ])
43
+ {
44
+ $ this ->applyCriteria ();
45
+ $ this ->applyScope ();
46
+
47
+ $ results = $ this ->model
48
+ ->with ($ with )
49
+ ->orderBy ($ orderByColumn , $ orderBy )
50
+ ->get ($ columns );
51
+
52
+ $ this ->resetModel ();
53
+ $ this ->resetScope ();
54
+
55
+ return $ results ;
56
+ }
57
+
58
+ /**
59
+ * Search data
60
+ *
61
+ * @param string|int $searchTerm
62
+ *
63
+ * @return mixed
64
+ */
65
+ public function search ($ searchTerm )
66
+ {
67
+ $ this ->applyCriteria ();
68
+ $ this ->applyScope ();
69
+
70
+ $ results = $ this ->model
71
+ ->whereLike ($ this ->searchFields (), $ searchTerm )
72
+ ->get ();
73
+
74
+ $ this ->resetModel ();
75
+ $ this ->resetScope ();
76
+
77
+ return $ results ;
78
+ }
79
+
80
+ /**
81
+ * Retrieve first data of repository
82
+ *
83
+ * @param array $columns
84
+ *
85
+ * @return mixed
86
+ */
87
+ public function first ($ columns = ['* ' ])
88
+ {
89
+ $ this ->applyCriteria ();
90
+ $ this ->applyScope ();
91
+
92
+ $ results = $ this ->model ->first ($ columns );
93
+
94
+ $ this ->resetModel ();
95
+
96
+ return $ results ;
97
+ }
98
+
99
+ /**
100
+ * Find data by id
101
+ *
102
+ * @param $id
103
+ * @param array $columns
104
+ *
105
+ * @return mixed
106
+ */
107
+ public function find ($ id , $ columns = ['* ' ])
108
+ {
109
+ $ this ->applyCriteria ();
110
+ $ this ->applyScope ();
111
+
112
+ $ results = $ this ->model ->findOrFail ($ id , $ columns );
113
+
114
+ $ this ->resetModel ();
115
+
116
+ return $ results ;
117
+ }
118
+
119
+ /**
120
+ * Find one data row by field and value
121
+ *
122
+ * @param string $field
123
+ * @param string $value
124
+ * @param array $columns
125
+ *
126
+ * @return mixed
127
+ */
128
+ public function findOneByField ($ field , $ value = null , $ columns = ['* ' ])
129
+ {
130
+ $ this ->applyCriteria ();
131
+ $ this ->applyScope ();
132
+
133
+ $ results = $ this ->model
134
+ ->where ($ field , '= ' , $ value )
135
+ ->first ($ columns );
136
+
137
+ $ this ->resetModel ();
138
+
139
+ return $ results ;
140
+ }
141
+
142
+ /**
143
+ * Find data by field and value
144
+ *
145
+ * @param string $field
146
+ * @param string $value
147
+ * @param array $columns
148
+ *
149
+ * @return mixed
150
+ */
151
+ public function findByField ($ field , $ value = null , $ columns = ['* ' ])
152
+ {
153
+ $ this ->applyCriteria ();
154
+ $ this ->applyScope ();
155
+
156
+ $ results = $ this ->model
157
+ ->where ($ field , '= ' , $ value )
158
+ ->get ($ columns );
159
+
160
+ $ this ->resetModel ();
161
+
162
+ return $ results ;
163
+ }
164
+
165
+ /**
166
+ * Find data by some params
167
+ *
168
+ * @param array $params
169
+ * @param array $columns
170
+ *
171
+ * @return mixed
172
+ */
173
+ public function findBy (array $ params , $ columns = ['* ' ])
174
+ {
175
+ $ this ->applyCriteria ();
176
+ $ this ->applyScope ();
177
+
178
+ $ results = $ this ->model
179
+ ->where ($ params )
180
+ ->first ($ columns );
181
+
182
+ $ this ->resetModel ();
183
+
184
+ return $ results ;
185
+ }
186
+
187
+ /**
188
+ * Find all data by some params
189
+ *
190
+ * @param array $params
191
+ * @param array $columns
192
+ *
193
+ * @return mixed
194
+ */
195
+ public function findAllBy (array $ params , $ columns = ['* ' ])
196
+ {
197
+ $ this ->applyCriteria ();
198
+ $ this ->applyScope ();
199
+
200
+ $ results = $ this ->model
201
+ ->where ($ params )
202
+ ->get ($ columns );
203
+
204
+ $ this ->resetModel ();
205
+
206
+ return $ results ;
207
+ }
208
+
209
+ /**
210
+ * Retrieve all data of repository, paginated
211
+ *
212
+ * @param null|int $limit
213
+ * @param array $columns
214
+ * @param string $method
215
+ *
216
+ * @return mixed
217
+ */
218
+ public function paginate ($ limit = null , $ columns = ['* ' ], $ method = "paginate " )
219
+ {
220
+ $ this ->applyCriteria ();
221
+ $ this ->applyScope ();
222
+
223
+ $ limit = is_null ($ limit )
224
+ ? config ('repository.pagination.limit ' , 15 )
225
+ : $ limit ;
226
+
227
+ $ results = $ this ->model ->{$ method }($ limit , $ columns );
228
+ $ results ->appends (app ('request ' )->query ());
229
+
230
+ $ this ->resetModel ();
7
231
232
+ return $ results ;
233
+ }
8
234
}
0 commit comments