Skip to content

Commit b2b5d5d

Browse files
committed
Add RepositoryQueryContract and his methods implementations in RepositoryQuery.php
1 parent a0e5c57 commit b2b5d5d

File tree

2 files changed

+240
-3
lines changed

2 files changed

+240
-3
lines changed

src/Contracts/Controls/RepositoryQueryContract.php

+14-3
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,23 @@ public function first($columns = ['*']);
5454
*/
5555
public function find($id, $columns = ['*']);
5656

57+
/**
58+
* Find one data row by field and value
59+
*
60+
* @param string $field
61+
* @param string $value
62+
* @param array $columns
63+
*
64+
* @return mixed
65+
*/
66+
public function findOneByField($field, $value = null, $columns = ['*']);
67+
5768
/**
5869
* Find data by field and value
5970
*
60-
* @param $field
61-
* @param $value
62-
* @param array $columns
71+
* @param string $field
72+
* @param string $value
73+
* @param array $columns
6374
*
6475
* @return mixed
6576
*/

src/Controls/RepositoryQuery.php

+226
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,233 @@
22

33
namespace Mawuekom\Repository\Controls;
44

5+
use Illuminate\Database\Eloquent\Builder;
6+
57
trait RepositoryQuery
68
{
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();
7231

232+
return $results;
233+
}
8234
}

0 commit comments

Comments
 (0)