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

ichmag17's avatar

Eloquent query doesn't return the right answer

I am trying to bring only some of the stations, but I can't seem to be using the query right. If i put it as query()->$results it says

Array to string conversion and this way it just returns all the stations

 public function builder()
    {
        $results = DB::select( DB::raw("
        SELECT * 
        FROM stations
        WHERE stations.id IN(
            SELECT station_id
            FROM station_workbook
            WHERE workbook_id = ".$this->current_workbook->id."
        )
    
    ") );

    return Station::query()->$results;

    }

0 likes
2 replies
ichmag17's avatar

I have updated the question and tried it and indeed now it works with your help.

Station::query()->whereHas('workbooks', fn ($query) => $query->where('workbook_id', $this->current_workbook->id));

this is what I have used in the end

Please or to participate in this conversation.