mirror of
https://github.com/pixelfed/pixelfed.git
synced 2024-11-11 00:54:50 +00:00
Extract all database calls that would occur in the template file to be done in the controller instead.
This commit is contained in:
parent
1b794c8508
commit
db7396e5c2
3 changed files with 31 additions and 10 deletions
|
@ -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'));
|
||||
}
|
||||
|
|
|
@ -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');
|
||||
|
|
|
@ -4,8 +4,7 @@
|
|||
|
||||
<div class="container">
|
||||
<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="row">
|
||||
<div class="col-12 col-md-8 status-photo">
|
||||
|
@ -50,7 +49,7 @@
|
|||
</span>
|
||||
</div>
|
||||
<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 class="timestamp mb-0">
|
||||
<p class="small text-uppercase mb-0"><a href="{{$status->url()}}" class="text-muted">{{$status->created_at->diffForHumans()}}</a></p>
|
||||
|
|
Loading…
Reference in a new issue