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

cankansu's avatar

Querybuilder executes the query twice

I have a code block that returns district name of a real estate listing but after I got the first record and try to get the name attribute of the record Laravel executes the query twice.

$district = DB::table('locations')
            ->whereRaw('st_within((select geom::geometry from listings where id = ' . $this->id . ' )::geometry,locations.geom::geometry) and admin_level = 8')
            ->select('name')
            ->first();

if (!is_null($district)) {
            return $district->name;
}

I got the district name in first query and try to get the name if district object is not null. But when I write $district->name code Laravel generates duplicate query.

0 likes
10 replies
bugsysha's avatar

Hard to tell on this code, but that should not happen. How are you checking for duplicate queries?

cankansu's avatar

I'm using telescope, when I comment return $district->name line, I see one query when I put it again query dublicates. Looks like it executes the again when I try to get the name.

martinbean's avatar

@cankansu Why not just fetch the name value if that’s what you want?

$districtName = DB::table('locations')
            ->whereRaw('st_within((select geom::geometry from listings where id = ' . $this->id . ' )::geometry,locations.geom::geometry) and admin_level = 8')
            ->select('name')
            ->value('name');

Reference: https://laravel.com/docs/8.x/queries#retrieving-a-single-row-column-from-a-table

Also, you should be using Eloquent models instead of “raw” query builder expressions.

bugsysha's avatar

Excellent suggestion. Totally forgot about value(). Thanks for reminding me.

cankansu's avatar

Hi all, I'm still working on the issue but I concluded that when I return anything but null, Accessor is being executed twice.

I tested it by writing following test;

public function getDistrictAttribute()
    {
        dump('test');
        return false;
    }

This code outputs "test" twice, but when I write the following

public function getDistrictAttribute()
    {
        dump('test');
        return null;
    }

code outputs "test" only once. For some reason functions is being executed twice.

Snapey's avatar

Why would an accessor be called - you are not using eloquent. You have a DB query not an eloquent model.

cankansu's avatar

I'm using GraphQL implementation for Laravel called lighthouse-php. When user asks for district attribute of a real estate listing I need to get the district polygon name which that listing resides. I'm using PostGIS of PostgreSQL and I couldn't find a way of using model relationships between Listing and Locations models.

These two models don't have foreign key relationship in the database, in some way I need to use st_within function of PostGIS to join them. Only way I came up is with Accessors.

Snapey's avatar

Your query uses DB facade it has no idea that there might be any eloquent models. So, if your accessor is being called, its not from the code you showed

cankansu's avatar

Probably yes, that is why I wrote dump('test') and see that accessor outputs "test" twice in the output when I return anything but null.

Snapey's avatar

Like I say - its got nothing to do with that DB query that sets $district

Perhaps you have more going off in the view or a view composer that is triggering this.

Please or to participate in this conversation.