Beginner
Beginner

Reputation: 1740

getting doubled queries laravel

I'm using laravel clockwork to monitor my queries

in my controller I have

public function index(){
    $errorFound = false;
    $error = ['error' => 'No Monitor Found'];
    $urls = $this->url->with('url_status','latestUrlStatus','users');
    if (request()->has('q')) {
        $keyword = '%'.request()->get('q').'%';
        $builder = $urls->where('description', 'like', $keyword);
        $builder->count() ? $urls = $builder : $errorFound = true;
    }
   return $errorFound === false ? UrlsResource::collection($urls->latest()->paginate(5)->appends(request()->query())) : $error;
}

on my laravel clockwork im getting doubled queries

enter image description here

is it normal? if it is a problem how can I fix this? TIA

Upvotes: 0

Views: 93

Answers (1)

patricus
patricus

Reputation: 62268

There's no problem. All of those queries are expected.

The first query (select users...) isn't from the code you've shown. It came from TrustProxies.

The second query (select count()) is from $builder->count().

All the rest of the queries come from $urls->latest()->paginate(5). The first thing paginate() does is run a count() query (the third query) to get the total number of records. Then it moves on to call the real queries.

In this case, the fourth query is your main query for all your urls, the fifth query is the query to eager load your url_status relationship, the sixth query is the query to eager load your latestUrlStatus relationship, and the seventh query is the query to eager load your users relationship.

Upvotes: 2

Related Questions