From db7396e5c2b62467d5de5ea8f1af385f63ca4687 Mon Sep 17 00:00:00 2001 From: Andrius Date: Fri, 1 Jun 2018 22:21:24 +0200 Subject: [PATCH] Extract all database calls that would occur in the template file to be done in the controller instead. --- app/Http/Controllers/StatusController.php | 23 ++++++++++++++++++----- app/Status.php | 13 +++++++++++-- resources/views/status/show.blade.php | 5 ++--- 3 files changed, 31 insertions(+), 10 deletions(-) diff --git a/app/Http/Controllers/StatusController.php b/app/Http/Controllers/StatusController.php index 3ad994eed..5cbc2a470 100644 --- a/app/Http/Controllers/StatusController.php +++ b/app/Http/Controllers/StatusController.php @@ -12,8 +12,21 @@ class StatusController extends Controller { public function show(Request $request, $username, int $id) { - $user = Profile::whereUsername($username)->firstOrFail(); - $status = Status::whereProfileId($user->id)->findOrFail($id); + /* + * Load all required data + */ + $user = Profile::whereUsername($username)->with('avatar')->firstOrFail(); + $status = Status::whereProfileId($user->id)->with(['firstMedia', 'comments', 'comments.profile'])->findOrFail($id); + + // avoid calling the same profile in the blade template + $status->profile = $user; + + // load a count non-relationship; this seems to be the most sane solution. + $status->likesCount; + /* + * End load all required data + */ + if(!$status->media_path && $status->in_reply_to_id) { return view('status.reply', compact('user', 'status')); } @@ -23,8 +36,8 @@ class StatusController extends Controller public function store(Request $request) { if(Auth::check() == false) - { - abort(403); + { + abort(403); } $user = Auth::user(); @@ -57,7 +70,7 @@ class StatusController extends Controller // TODO: Parse Caption // TODO: Send to subscribers - + return redirect($status->url()); } } diff --git a/app/Status.php b/app/Status.php index aa1146280..3b597f0d6 100644 --- a/app/Status.php +++ b/app/Status.php @@ -20,7 +20,7 @@ class Status extends Model public function firstMedia() { - return $this->hasMany(Media::class)->orderBy('order', 'asc')->first(); + return $this->hasOne(Media::class)->orderBy('order', 'asc'); } public function thumb() @@ -37,7 +37,7 @@ class Status extends Model public function mediaUrl() { - $path = $this->firstMedia()->media_path; + $path = $this->firstMedia->media_path; $url = Storage::url($path); return url($url); } @@ -47,6 +47,15 @@ class Status extends Model return $this->hasMany(Like::class); } + public function getLikesCountAttribute() + { + if(isset($this->likesCountNumber)) { + return $this->likesCountNumber; + } + + return $this->likesCountNumber = $this->likes()->count(); + } + public function comments() { return $this->hasMany(Status::class, 'in_reply_to_id'); diff --git a/resources/views/status/show.blade.php b/resources/views/status/show.blade.php index 667eabc05..b7125bfd1 100644 --- a/resources/views/status/show.blade.php +++ b/resources/views/status/show.blade.php @@ -4,8 +4,7 @@
- -
+
@@ -50,7 +49,7 @@