Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

lat4732's avatar
Level 12

Duplicated query because of json-ld

Hey!

I'm inserting json-ld into my pages but I faced a problem. On the page where I show reviews for a particular website, those same reviews are taken from an AJAX call, which takes HTML formatted code directly, which puts it in a div. I actually need these reviews details in the json-ld script tag. But now, in order to achieve what I want, I have to make a new (same) query to the database which is used in the AJAX call to extract the necessary information and integrate it into the json-ld script tag. Do you have any idea how to merge the two without repeating the query?

0 likes
8 replies
Braunson's avatar

@Laralex That depends on how long you are caching them for and also how frequently your content is updated. If you cache it for 5 minutes, and then after it expired, re-cache it with a fresh query it's only ever 5 mins max out of date.

lat4732's avatar
Level 12

@Braunson Yeah, you're right. But the fact I repeat the query is pissing me off.

Sinnbeck's avatar

Consider showing some code if you want help with how to change it

1 like
lat4732's avatar
Level 12

This is the endpoint that the AJAX call is hitting:

$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->sort, function($builder, $sort) {
        $builder->when($sort == "useful", function($builder) use ($sort) {
            $builder->orderBy($sort, 'desc');
        }, function($builder) use ($sort) {
            $builder->orderBy('created_at', $sort);
        });
    }, function($builder) {
        $builder->orderBy('created_at', 'desc');
    })
    ->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}%"]);
        });
    })
    ->paginate(10);

return response()->json([
    'pagination' => collect($reviews->toArray())->except('data'),
    'html' => view('ajaxViews.reviews', [
        'website' => $website,
        'reviews' => $reviews
    ])->render()
]);

That's how I display the HTML returned from the endpoint

function searchUrl(page) {
            return '{{ url("/get/" . $website->web_seo_url . "/reviews") }}?' + $("#filterReviewsForm").serialize() + '&page=' + page;
}

function showReviews(page = 1) {

			return $.get(searchUrl(page)).done(function (reviews) {
				
				$("#reviews-box").html(reviews.html).ready(function () {
                                 ...........
                });

           });

}

And that's how my json-ld should look like:

<script type="application/ld+json">
    {
        "@context" : "https://schema.org/",
         // .........
        @if($website->reviews)  
            @foreach($website->reviews()) // which is a duplicate of the query that is getting the reviews for the website and its slowing so much the page because there are many reviews
            // ....... some json-ld content
            @endforeach
        @endif
    }
</script>

How to prevent duplicating this big query?

Please or to participate in this conversation.