Skip to content

Commit

Permalink
optimized DB query for article and author
Browse files Browse the repository at this point in the history
  • Loading branch information
sawirricardo committed May 19, 2021
1 parent c7b8994 commit 4f38943
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 9 deletions.
7 changes: 3 additions & 4 deletions app/Http/Livewire/Front/User/Show.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,17 @@ class Show extends Component
public function updatedViewingFavoriteArticles()
{
if ($this->viewingFavoriteArticles) {
$this->articles = $this->user->favorites(\App\Models\Article::class)->get();
$this->articles = $this->user->favorites(\App\Models\Article::class)->with(['author'])->get();
}

if (!$this->viewingFavoriteArticles) {
$this->articles = \App\Models\Article::where('user_id', '=', $this->user->id)->get();
$this->articles = \App\Models\Article::where('user_id', '=', $this->user->id)->with(['author'])->get();
}
}

public function mount(\App\Models\User $user)
{
$user->load(['articles']);
$this->articles = $user->articles;
$this->articles = \App\Models\Article::where('user_id', '=', $this->user->id)->with(['author'])->get();
$this->user = $user;
$this->loggedInUser = \App\Models\User::find(auth()->id());

Expand Down
4 changes: 2 additions & 2 deletions resources/views/livewire/front/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ class="nav-link {{ $viewingPrivateFeed ? '':'active' }}" href="#">Global Feed</a
class="author">{{ $article->author->name }}</a>
<span class="date">{{ $article->created_at }}</span>
</div>
<button class="btn btn-outline-primary btn-sm pull-xs-right">
{{-- <button class="btn btn-outline-primary btn-sm pull-xs-right">
<i class="ion-heart"></i> {{ $article->favoritersCountReadable() }}
</button>
</button> --}}
</div>
<a href="{{ route('front.article.show',['article' => $article->slug]) }}" class="preview-link">
<h1>{{ $article->title }}</h1>
Expand Down
6 changes: 3 additions & 3 deletions resources/views/livewire/front/user/show.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ class="author">{{ $article->author->name }}</a>
<span class="date">{{ $article->created_at }}</span>
</div>

@guest
{{-- @guest
<a href="{{ route('app.login') }}" class="btn btn-outline-primary btn-sm pull-xs-right">
<i class="ion-heart"></i> {{ $article->favoritersCountReadable() }}
<i class="ion-heart"></i> {{ $article->favoritersCountReadable() }}
</a>
@endguest
Expand All @@ -84,7 +84,7 @@ class="author">{{ $article->author->name }}</a>
<i class="ion-heart"></i> {{ $article->favoritersCountReadable() }}
</button>
@endif
@endauth
@endauth --}}
</div>
<a href="{{ route('front.article.show',['article'=>$article->slug]) }}" class="preview-link">
<h1>{{ $article->title }}</h1>
Expand Down

0 comments on commit 4f38943

Please sign in to comment.