mirror of
https://github.com/Kovah/LinkAce.git
synced 2025-04-22 07:52:43 +02:00
Optimize database queries by properly loading relationships
This commit is contained in:
parent
30d2fc1032
commit
0743f4b0c5
@ -18,6 +18,7 @@ class LinkController extends Controller
|
||||
public function index(Request $request): View
|
||||
{
|
||||
$links = Link::privateOnly(false)
|
||||
->with('tags')
|
||||
->orderBy(
|
||||
$request->input('orderBy', 'created_at'),
|
||||
$request->input('orderDir', 'desc')
|
||||
|
@ -24,6 +24,7 @@ class LinkController extends Controller
|
||||
public function index(Request $request): View
|
||||
{
|
||||
$links = Link::byUser(auth()->id())
|
||||
->with('tags')
|
||||
->orderBy(
|
||||
$request->input('orderBy', 'created_at'),
|
||||
$request->input('orderDir', 'desc')
|
||||
|
@ -39,6 +39,7 @@ use Venturecraft\Revisionable\RevisionableTrait;
|
||||
* @property Collection|Tag[] $tags
|
||||
* @property User $user
|
||||
* @method static Builder|Link byUser($user_id)
|
||||
* @method static Builder|Link privateOnly($is_private)
|
||||
* @method static MorphMany revisionHistory()
|
||||
*/
|
||||
class Link extends Model
|
||||
@ -93,24 +94,24 @@ class Link extends Model
|
||||
* Scope for the user relation
|
||||
*
|
||||
* @param Builder $query
|
||||
* @param int $user_id
|
||||
* @param int $userId
|
||||
* @return mixed
|
||||
*/
|
||||
public function scopeByUser($query, $user_id)
|
||||
public function scopeByUser($query, $userId)
|
||||
{
|
||||
return $query->where('user_id', $user_id);
|
||||
return $query->where('user_id', $userId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Scope for the user relation
|
||||
*
|
||||
* @param Builder $query
|
||||
* @param bool $is_private
|
||||
* @param bool $isPrivate
|
||||
* @return mixed
|
||||
*/
|
||||
public function scopePrivateOnly($query, bool $is_private)
|
||||
public function scopePrivateOnly($query, bool $isPrivate)
|
||||
{
|
||||
return $query->where('is_private', $is_private);
|
||||
return $query->where('is_private', $isPrivate);
|
||||
}
|
||||
|
||||
/*
|
||||
@ -181,16 +182,13 @@ class Link extends Model
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function domainOfURL()
|
||||
public function domainOfURL(): string
|
||||
{
|
||||
$urlDetails = parse_url($this->url);
|
||||
return $urlDetails['host'] ?? $this->shortUrl(20);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return null|string
|
||||
*/
|
||||
public function tagsForInput()
|
||||
public function tagsForInput(): ?string
|
||||
{
|
||||
$tags = $this->tags;
|
||||
|
||||
@ -201,10 +199,7 @@ class Link extends Model
|
||||
return $tags->implode('name', ',');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return null|string
|
||||
*/
|
||||
public function listsForInput()
|
||||
public function listsForInput(): ?string
|
||||
{
|
||||
$lists = $this->lists;
|
||||
|
||||
@ -215,10 +210,6 @@ class Link extends Model
|
||||
return $lists->implode('name', ',');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|null $additionalClasses
|
||||
* @return string
|
||||
*/
|
||||
public function getIcon(string $additionalClasses = ''): string
|
||||
{
|
||||
if ($this->icon === null) {
|
||||
@ -257,7 +248,7 @@ class Link extends Model
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function addedAt()
|
||||
public function addedAt(): string
|
||||
{
|
||||
$output = '<time-ago class="cursor-help"';
|
||||
$output .= ' datetime="' . $this->created_at->toIso8601String() . '"';
|
||||
|
@ -16,8 +16,8 @@
|
||||
@if($list->description)
|
||||
<p>{{ $list->description }}</p>
|
||||
@endif
|
||||
@if($list->links->count() > 0)
|
||||
{{ trans_choice('list.number_links', $list->links->count(), ['number' => $list->links->count()]) }}
|
||||
@if($list->links_count > 0)
|
||||
{{ trans_choice('list.number_links', $list->links_count, ['number' => $list->links_count]) }}
|
||||
@else
|
||||
<span class="text-muted">@lang('link.no_links')</span>
|
||||
@endif
|
||||
|
@ -5,6 +5,6 @@
|
||||
</a>
|
||||
</td>
|
||||
<td>
|
||||
{{ $tag->links->count() }}
|
||||
{{ $tag->links_count }}
|
||||
</td>
|
||||
</tr>
|
||||
|
@ -19,8 +19,8 @@
|
||||
@if($list->description)
|
||||
<p>{{ $list->description }}</p>
|
||||
@endif
|
||||
@if($list->links->count() > 0)
|
||||
{{ trans_choice('list.number_links', $list->links->count(), ['number' => $list->links->count()]) }}
|
||||
@if($list->links_count > 0)
|
||||
{{ trans_choice('list.number_links', $list->links_count, ['number' => $list->links_count]) }}
|
||||
@else
|
||||
<span class="text-muted">@lang('link.no_links')</span>
|
||||
@endif
|
||||
|
@ -11,7 +11,7 @@
|
||||
</a>
|
||||
</td>
|
||||
<td>
|
||||
{{ $tag->links->count() }}
|
||||
{{ $tag->links_count }}
|
||||
</td>
|
||||
<td class="text-right">
|
||||
<div class="btn-group btn-group-xs">
|
||||
|
Loading…
x
Reference in New Issue
Block a user