Skip to content

Commit 35c75ac

Browse files
committed
firstOrFail method implemented, smartPaginate limit bug fixed
1 parent d60d431 commit 35c75ac

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

src/Contracts/RepositoryInterface.php

+21
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,27 @@ public function detach($id, $relation, $ids);
117117
*/
118118
public function findOrFail($id, $columns = ['*']);
119119

120+
/**
121+
* Find a model by its primary key or throw an exception and return repository instance.
122+
*
123+
* @param mixed $id
124+
* @param array $columns
125+
* @return \AwesIO\Repository\Contracts\RepositoryInterface
126+
*
127+
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
128+
*/
129+
public function findOrFailRepo($id, $columns = ['*']);
130+
131+
/**
132+
* Execute the query and get the first result or throw an exception.
133+
*
134+
* @param array $columns
135+
* @return \Illuminate\Database\Eloquent\Model|static
136+
*
137+
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
138+
*/
139+
public function firstOrFail($columns = ['*']);
140+
120141
/**
121142
* Paginate the given query by 'limit' request parameter
122143
* @return mixed

src/Eloquent/BaseRepository.php

+19
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,23 @@ public function findOrFailRepo($id, $columns = ['*'])
213213
return $this;
214214
}
215215

216+
/**
217+
* Execute the query and get the first result or throw an exception.
218+
*
219+
* @param array $columns
220+
* @return \Illuminate\Database\Eloquent\Model|static
221+
*
222+
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
223+
*/
224+
public function firstOrFail($columns = ['*'])
225+
{
226+
$results = $this->entity->firstOrFail($columns);
227+
228+
$this->reset();
229+
230+
return $results;
231+
}
232+
216233
/**
217234
* Paginate the given query by 'limit' request parameter
218235
* @return mixed
@@ -224,6 +241,8 @@ public function smartPaginate()
224241
config('awesio-repository.smart_paginate.default_limit')
225242
);
226243

244+
if ($limit === 0) $limit = config('awesio-repository.smart_paginate.default_limit');
245+
227246
$maxLimit = config('awesio-repository.smart_paginate.max_limit');
228247

229248
$limit = ($limit <= $maxLimit) ? $limit : $maxLimit;

tests/Unit/RepositoryTest.php

+18
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,24 @@ public function it_finds_and_returns_respository_instance_or_fails()
285285
$repository->findOrFailRepo(22);
286286
}
287287

288+
/** @test */
289+
public function it_finds_first_or_fails()
290+
{
291+
$model = factory(Model::class)->create();
292+
293+
$repository = new Repository;
294+
295+
$result = $repository->where('id', $model->id)->firstOrFail();
296+
297+
$this->assertInstanceOf(Model::class, $result);
298+
299+
$this->assertEquals($model->name, $result->name);
300+
301+
$this->expectException(ModelNotFoundException::class);
302+
303+
$repository->where('id', $model->id + 1)->firstOrFail();
304+
}
305+
288306
/** @test */
289307
public function it_can_smart_paginate()
290308
{

0 commit comments

Comments
 (0)