Skip to content
This repository has been archived by the owner on Aug 7, 2019. It is now read-only.

Commit

Permalink
Added findOrFail() method (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
d8vjork authored and kaidesu committed Jul 2, 2018
1 parent 8faadfc commit 5c15600
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/Contracts/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ public function findBy($attribute, $value, $columns = ['*'], $with = []);
*/
public function findAll($columns = ['*'], $with = []);

/**
* Find an entity by its primary key or fail if it doesn't exist.
*
* @param int $id
* @param array $columns
* @param array $with
*/
public function findOrFail($id, $columns = ['*'], $with = []);

/**
* Find all entities matching where conditions.
*
Expand Down
17 changes: 17 additions & 0 deletions src/Repositories/EloquentRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,23 @@ public function findAll($columns = ['*'], $with = [])
});
}

/**
* Find an entity by its primary key or fail if it doesn't exist.
*
* @param int $id
* @param array $columns
* @param array $with
*/
public function findOrFail($id, $columns = ['*'], $with = [])
{
$cacheKey = $this->generateKey([$id, $columns, $with]);

return $this->cacheResults(get_called_class(), __FUNCTION__, $cacheKey, function () use ($id, $columns, $with) {
return $this->model->with($with)
->findOrFail($id, $columns);
});
}

/**
* Find all entities matching where conditions.
*
Expand Down

0 comments on commit 5c15600

Please sign in to comment.