@splatEric I'm stucked again. I tried to implement what we have done in another blade file which has the same construction but without success. What we've done was for report modals which are stored on the main layout blade file before the </body> tag. But the reviews are actually loaded from AJAX call which is hitting this controller method:
$reviews = $website->reviews()->where('report_status', '!=', 3)->with('reply', 'reports', 'media', 'user')
->when($request->stars, function($builder, $stars) {
$builder->when(is_array($stars), function($query) use ($stars) {
$query->where(function($query) use ($stars) {
foreach($stars as $star) {
$query->orWhere('review_stars', $star);
}
});
}, function($query) use($stars) {
$query->where('review_stars', $stars);
});
})
->when($request->search, function($builder, $search) {
$builder->where(function($query) use ($search) {
$query->orWhereRaw('LOWER(review_header) LIKE LOWER(?)', ["%{$search}%"])
->orWhereRaw('LOWER(review_content) LIKE LOWER(?)', ["%{$search}%"]);
});
})
->reorder()
->when(
$request->sort,
function ($builder, $sort) {
$builder->when(
$sort == "useful",
fn ($builder) => $builder->orderBy('useful', 'desc'),
fn($builder) =>$builder->orderBy('created_at', $sort)
);
},
fn ($builder) => $builder->orderByRaw("user_id = ? DESC, created_at DESC", [auth()->id()])
)
->paginate(15);
$user_have_opened_reports = 0;
if(auth()->check()) {
auth()->user()->with('reviewReports');
$available_review_ids = $reviews->pluck('id');
$user_have_opened_reports = auth()->user()->reviewReports()->whereIn('review_id', $available_review_ids)->where('solved', 0)->doesntExist();
}
return response()->json([
'pagination' => collect($reviews->toArray())->except('data'),
'html' => view('website.ajaxViews.reviews', [
'website' => $website,
'reviews' => $reviews
])->render(),
'user_have_opened_reports' => (auth()->check()) ? $user_have_opened_reports : []
]);
and the following blade file is displayed in the main layout blade file through the AJAX call
// .............. more code
@auth
@if(auth()->user()->id != $review->user_id && auth()->user()->is_company != 1)
@if(auth()->user()->reviewReports->where('review_id', $review->id)->count() < count(config('app.report_reasons')))
@if(auth()->user()->reviewReports()->where('review_id', $review->id)->where('solved', 0)->doesntExist())
<a href="javascript:void(0);" class="ReportReview jikh8000_{{ $review->id }}" data-id="{{ $review->id }}" data-toggle="tooltip" title="@lang('main.reviews_report_this_review')" data-bs-toggle="modal" data-bs-target="#reportModal{{ $review->id }}"><i class="icon-flag text-danger"></i></a></li>
@endif
@endif
@endif
@endauth
// .............. more code
and I'm stucked now. On a page that has 26 reviews, this line is executing like 28-29 queries.
@if(auth()->user()->reviewReports()->where('review_id', $review->id)->where('solved', 0)->doesntExist())
How can I replace it with something like what we've done above? I've tried to do something as you can see in the controller method but I have no idea how to implement it in the if statement.