Skip to content

Commit d9a1a92

Browse files
Merge pull request #161 from radiate-framework/feature/query-builder
Feature/query builder
2 parents 5063159 + 1ad0007 commit d9a1a92

3 files changed

Lines changed: 38 additions & 9 deletions

File tree

src/Cache/resources/config/cache.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
| Cache Key Prefix
88
|--------------------------------------------------------------------------
99
|
10-
| We'll specify a value to get prefixed to all our keys so we can avoid
11-
| collisions with plugins that migt be setting the same keys.
10+
| We'll specify a value to get prefixed to all our keys so we can avoid
11+
| collisions with plugins that might be setting the same keys.
1212
|
1313
*/
1414

src/Database/Builder.php

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,14 +323,46 @@ public function only($columns)
323323
/**
324324
* Get the first record
325325
*
326-
* @param string $column
327326
* @return \Radiate\Database\Model|mixed
328327
*/
329328
public function first()
330329
{
331330
return $this->take(1)->get()->first();
332331
}
333332

333+
/**
334+
* Get the first record or throw an exception
335+
*
336+
* @return \Radiate\Database\Model|mixed
337+
*
338+
* @throws \Radiate\Database\ModelNotFoundException
339+
*/
340+
public function firstOrFail()
341+
{
342+
if ($first = $this->first()) {
343+
return $first;
344+
}
345+
346+
throw new ModelNotFoundException(get_class($this->model));
347+
}
348+
349+
/**
350+
* Get the first record or throw an exception
351+
*
352+
* @param int $id
353+
* @return \Radiate\Database\Model|mixed
354+
*
355+
* @throws \Radiate\Database\ModelNotFoundException
356+
*/
357+
public function findOrFail(int $id)
358+
{
359+
if ($found = $this->find($id)) {
360+
return $found;
361+
}
362+
363+
throw new ModelNotFoundException(get_class($this->model));
364+
}
365+
334366
/**
335367
* Dynamically handle calls into the query instance.
336368
*

src/Database/ModelNotFoundException.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22

33
namespace Radiate\Database;
44

5-
use Radiate\Foundation\Http\Exceptions\HttpResponseException;
6-
use Radiate\Http\Response;
5+
use RuntimeException;
76

8-
class ModelNotFoundException extends HttpResponseException
7+
class ModelNotFoundException extends RuntimeException
98
{
109
/**
1110
* Create a new model not found exception.
@@ -15,8 +14,6 @@ class ModelNotFoundException extends HttpResponseException
1514
*/
1615
public function __construct(string $model)
1716
{
18-
parent::__construct(
19-
new Response("No query results for model [{$model}]", 404)
20-
);
17+
parent::__construct("No query results for model [{$model}]", 404);
2118
}
2219
}

0 commit comments

Comments
 (0)