Extract all database calls that would occur in the template file to be done in the controller instead.

This commit is contained in:
Andrius 2018-06-01 22:21:24 +02:00
parent 1b794c8508
commit db7396e5c2
3 changed files with 31 additions and 10 deletions

View file

@ -12,8 +12,21 @@ class StatusController extends Controller
{ {
public function show(Request $request, $username, int $id) 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) { if(!$status->media_path && $status->in_reply_to_id) {
return view('status.reply', compact('user', 'status')); return view('status.reply', compact('user', 'status'));
} }

View file

@ -20,7 +20,7 @@ class Status extends Model
public function firstMedia() public function firstMedia()
{ {
return $this->hasMany(Media::class)->orderBy('order', 'asc')->first(); return $this->hasOne(Media::class)->orderBy('order', 'asc');
} }
public function thumb() public function thumb()
@ -37,7 +37,7 @@ class Status extends Model
public function mediaUrl() public function mediaUrl()
{ {
$path = $this->firstMedia()->media_path; $path = $this->firstMedia->media_path;
$url = Storage::url($path); $url = Storage::url($path);
return url($url); return url($url);
} }
@ -47,6 +47,15 @@ class Status extends Model
return $this->hasMany(Like::class); return $this->hasMany(Like::class);
} }
public function getLikesCountAttribute()
{
if(isset($this->likesCountNumber)) {
return $this->likesCountNumber;
}
return $this->likesCountNumber = $this->likes()->count();
}
public function comments() public function comments()
{ {
return $this->hasMany(Status::class, 'in_reply_to_id'); return $this->hasMany(Status::class, 'in_reply_to_id');

View file

@ -4,8 +4,7 @@
<div class="container"> <div class="container">
<div class="col-12 mt-4"> <div class="col-12 mt-4">
<div class="card status-container orientation-{{$status->firstMedia->orientation ?? 'unknown'}}">
<div class="card status-container orientation-{{$status->firstMedia()->orientation ?? 'unknown'}}">
<div class="card-body p-0"> <div class="card-body p-0">
<div class="row"> <div class="row">
<div class="col-12 col-md-8 status-photo"> <div class="col-12 col-md-8 status-photo">
@ -50,7 +49,7 @@
</span> </span>
</div> </div>
<div class="likes font-weight-bold mb-0"> <div class="likes font-weight-bold mb-0">
<span class="like-count">{{$status->likes()->count()}}</span> likes <span class="like-count">{{$status->likesCount}}</span> likes
</div> </div>
<div class="timestamp mb-0"> <div class="timestamp mb-0">
<p class="small text-uppercase mb-0"><a href="{{$status->url()}}" class="text-muted">{{$status->created_at->diffForHumans()}}</a></p> <p class="small text-uppercase mb-0"><a href="{{$status->url()}}" class="text-muted">{{$status->created_at->diffForHumans()}}</a></p>