Skip to content

Commit

Permalink
Merge pull request #161 from radiate-framework/feature/query-builder
Browse files Browse the repository at this point in the history
Feature/query builder
  • Loading branch information
BenRutlandWeb authored Oct 23, 2021
2 parents 5063159 + 1ad0007 commit d9a1a92
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/Cache/resources/config/cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
| Cache Key Prefix
|--------------------------------------------------------------------------
|
| We'll specify a value to get prefixed to all our keys so we can avoid
| collisions with plugins that migt be setting the same keys.
| We'll specify a value to get prefixed to all our keys so we can avoid
| collisions with plugins that might be setting the same keys.
|
*/

Expand Down
34 changes: 33 additions & 1 deletion src/Database/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,14 +323,46 @@ public function only($columns)
/**
* Get the first record
*
* @param string $column
* @return \Radiate\Database\Model|mixed
*/
public function first()
{
return $this->take(1)->get()->first();
}

/**
* Get the first record or throw an exception
*
* @return \Radiate\Database\Model|mixed
*
* @throws \Radiate\Database\ModelNotFoundException
*/
public function firstOrFail()
{
if ($first = $this->first()) {
return $first;
}

throw new ModelNotFoundException(get_class($this->model));
}

/**
* Get the first record or throw an exception
*
* @param int $id
* @return \Radiate\Database\Model|mixed
*
* @throws \Radiate\Database\ModelNotFoundException
*/
public function findOrFail(int $id)
{
if ($found = $this->find($id)) {
return $found;
}

throw new ModelNotFoundException(get_class($this->model));
}

/**
* Dynamically handle calls into the query instance.
*
Expand Down
9 changes: 3 additions & 6 deletions src/Database/ModelNotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

namespace Radiate\Database;

use Radiate\Foundation\Http\Exceptions\HttpResponseException;
use Radiate\Http\Response;
use RuntimeException;

class ModelNotFoundException extends HttpResponseException
class ModelNotFoundException extends RuntimeException
{
/**
* Create a new model not found exception.
Expand All @@ -15,8 +14,6 @@ class ModelNotFoundException extends HttpResponseException
*/
public function __construct(string $model)
{
parent::__construct(
new Response("No query results for model [{$model}]", 404)
);
parent::__construct("No query results for model [{$model}]", 404);
}
}

0 comments on commit d9a1a92

Please sign in to comment.