Skip to content

Commit

Permalink
Add eager load for updatedBy.group and withTrashed
Browse files Browse the repository at this point in the history
  • Loading branch information
Obi-Wana committed Oct 31, 2024
1 parent a71fef5 commit 8c0edf7
Show file tree
Hide file tree
Showing 5 changed files with 5 additions and 8 deletions.
2 changes: 1 addition & 1 deletion app/Http/Controllers/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public function index(Request $request): \Illuminate\Contracts\View\Factory|\Ill
'latest_posts:by-group:'.auth()->user()->group_id,
$expiresAt,
fn () => Post::query()
->with('user', 'user.group', 'topic:id,name')
->with('user', 'user.group', 'topic:id,name', 'updatedBy.group')
->withCount('likes', 'dislikes', 'authorPosts', 'authorTopics')
->withSum('tips', 'bon')
->withExists([
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/User/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function index(User $user): \Illuminate\Contracts\View\Factory|\Illuminat
return view('user.post.index', [
'user' => $user,
'posts' => $user->posts()
->with('user', 'user.group', 'topic:id,name,state')
->with('user', 'user.group', 'topic:id,name,state', 'updatedBy.group')
->withCount('likes', 'dislikes', 'authorPosts', 'authorTopics')
->withSum('tips', 'bon')
->authorized(canReadTopic: true)
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Livewire/PostSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ final public function updatingSearch(): void
final public function posts(): \Illuminate\Pagination\LengthAwarePaginator
{
return Post::query()
->with('user', 'user.group', 'topic:id,name,state')
->with('user', 'user.group', 'topic:id,name,state', 'updatedBy.group')
->withCount('likes', 'dislikes', 'authorPosts', 'authorTopics')
->withSum('tips', 'bon')
->withExists([
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Livewire/TopicPostSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ final public function updatingSearch(): void
final public function posts(): \Illuminate\Pagination\LengthAwarePaginator
{
$posts = Post::query()
->with('user', 'user.group')
->with('user', 'user.group', 'updatedBy.group')
->withCount('likes', 'dislikes', 'authorPosts', 'authorTopics')
->withSum('tips', 'bon')
->where('topic_id', '=', $this->topic->id)
Expand Down
5 changes: 1 addition & 4 deletions app/Models/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,7 @@ public function user(): \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function updatedBy(): \Illuminate\Database\Eloquent\Relations\BelongsTo
{
return $this->belongsTo(User::class, 'updated_by', 'id')->withDefault([
'username' => 'System',
'id' => User::SYSTEM_USER_ID,
]);
return $this->belongsTo(User::class, 'updated_by', 'id')->withTrashed();
}

/**
Expand Down

0 comments on commit 8c0edf7

Please sign in to comment.