Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(galleries): more efficient queries #1098

Merged
merged 2 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/Http/Controllers/GalleryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public function getGallery($id, Request $request) {
return view('galleries.gallery', [
'gallery' => $gallery,
'submissions' => $query->paginate(20)->appends($request->query()),
'prompts' => [0 => 'Any Prompt'] + Prompt::whereIn('id', GallerySubmission::where('gallery_id', $gallery->id)->visible(Auth::check() ? Auth::user() : null)->accepted()->whereNotNull('prompt_id')->pluck('prompt_id')->toArray())->orderBy('name')->pluck('name', 'id')->toArray(),
'prompts' => [0 => 'Any Prompt'] + Prompt::whereIn('id', GallerySubmission::where('gallery_id', $gallery->id)->visible(Auth::check() ? Auth::user() : null)->accepted()->whereNotNull('prompt_id')->select('prompt_id')->distinct()->pluck('prompt_id')->toArray())->orderBy('name')->pluck('name', 'id')->toArray(),
'childSubmissions' => GallerySubmission::whereIn('gallery_id', $gallery->children->pluck('id')->toArray())->where('is_visible', 1)->where('status', 'Accepted'),
'galleryPage' => true,
'sideGallery' => $gallery,
Expand Down
4 changes: 2 additions & 2 deletions resources/views/galleries/gallery.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
</p>
@endif
<p>{!! $gallery->description !!}</p>
@if (!$gallery->submissions->count() && $gallery->children->count() && $childSubmissions->count())
@if (!$gallery->submissions()->count() && $gallery->children->count() && $childSubmissions->count())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the reasoning here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without the parenthesis it's pulling all submissions into memory to count them instead of letting sql count them

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought it was the reverse? Or is there some particular nuance of relation-vs-query I'm missing here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahhhh I see

<p>This gallery has no submissions; instead, displayed is a selection of the most recent submissions from its sub-galleries. Please navigate to one of the sub-galleries to view more.</p>
@endif

Expand Down Expand Up @@ -59,7 +59,7 @@
{!! Form::close() !!}
</div>

@if ($gallery->submissions->count())
@if ($gallery->submissions()->count())
{!! $submissions->render() !!}

<div class="d-flex align-content-around flex-wrap mb-2">
Expand Down