From 64f541d30df0ce202b223045aed06c7108d09ffb Mon Sep 17 00:00:00 2001 From: Jonny Barnes Date: Wed, 9 Aug 2017 22:15:45 +0100 Subject: [PATCH] Change some first Eloquent methods to firstOrFail --- app/Http/Controllers/ArticlesController.php | 2 +- app/Http/Controllers/MicropubClientController.php | 2 +- app/Http/Controllers/MicropubController.php | 5 +++-- app/Http/Controllers/NotesController.php | 2 +- 4 files changed, 6 insertions(+), 5 deletions(-) diff --git a/app/Http/Controllers/ArticlesController.php b/app/Http/Controllers/ArticlesController.php index 9f0c1afd..77a35ef5 100644 --- a/app/Http/Controllers/ArticlesController.php +++ b/app/Http/Controllers/ArticlesController.php @@ -29,7 +29,7 @@ class ArticlesController extends Controller */ public function show($year, $month, $slug) { - $article = Article::where('titleurl', $slug)->first(); + $article = Article::where('titleurl', $slug)->firstOrFail(); if ($article->updated_at->year != $year || $article->updated_at->month != $month) { throw new \Exception; } diff --git a/app/Http/Controllers/MicropubClientController.php b/app/Http/Controllers/MicropubClientController.php index 5ceaa781..f1605627 100644 --- a/app/Http/Controllers/MicropubClientController.php +++ b/app/Http/Controllers/MicropubClientController.php @@ -269,7 +269,7 @@ class MicropubClientController extends Controller ]; if ($request->session()->has('me')) { $data['me'] = normalize_url($request->session()->get('me')); - $user = IndieWebUser::where('me', $request->session()->get('me'))->first(); + $user = IndieWebUser::where('me', $request->session()->get('me'))->firstOrFail(); $data['token'] = $user->token ?? 'none defined'; $data['syndication'] = $user->syndication ?? 'none defined'; $data['media-endpoint'] = $user->mediaEndpoint ?? 'none defined'; diff --git a/app/Http/Controllers/MicropubController.php b/app/Http/Controllers/MicropubController.php index f155548d..a8322d88 100644 --- a/app/Http/Controllers/MicropubController.php +++ b/app/Http/Controllers/MicropubController.php @@ -9,6 +9,7 @@ use Monolog\Handler\StreamHandler; use Illuminate\Http\{Request, Response}; use App\Exceptions\InvalidTokenException; use Phaza\LaravelPostgis\Geometries\Point; +use Illuminate\Database\Eloquent\ModelNotFoundException; use Ramsey\Uuid\Exception\UnsatisfiedDependencyException; use App\Services\{NoteService, PlaceService, TokenService}; @@ -214,8 +215,8 @@ class MicropubController extends Controller //is it a note we are updating? if (mb_substr($urlPath, 1, 5) === 'notes') { try { - $note = Note::nb60(basename($urlPath))->first(); - } catch (\Exception $exception) { + $note = Note::nb60(basename($urlPath))->firstOrFail(); + } catch (ModelNotFoundException $exception) { return response()->json([ 'error' => 'invalid_request', 'error_description' => 'No known note with given ID', diff --git a/app/Http/Controllers/NotesController.php b/app/Http/Controllers/NotesController.php index 548cea0a..93fecc23 100644 --- a/app/Http/Controllers/NotesController.php +++ b/app/Http/Controllers/NotesController.php @@ -35,7 +35,7 @@ class NotesController extends Controller */ public function show($urlId) { - $note = Note::nb60($urlId)->with('webmentions')->first(); + $note = Note::nb60($urlId)->with('webmentions')->firstOrFail(); return view('notes.show', compact('note')); }