File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff 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 *
Original file line number Diff line number Diff line change 22
33namespace 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}
You can’t perform that action at this time.
0 commit comments