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

vincej's avatar
Level 15

What is Wrong with my Query Builder Join - it won't give me the data I get in raw SQL ?

Ok, so I create a raw SQL join to make certain I have got it right. It works great, here it is:

Select contractors.crew_id,  count(*) as members, skill_type.skill, location_name,crew_name
from contractors
join auburntree.crews
on contractors.crew_id=crews.id
join skill_type
on skill_type.skill_id = crews.skill_id
join location 
on location.id = crews.location_id
GROUP BY contractors.crew_id;

Now I know it works, I want to apply it to Query Builder. This also works .... to a fashion. In the RAW version I get all the data in the SELECT. However, when I do a dd() of the QB $Data, I get just the contractors.crew_id and nothing else. I don't know what Newb mistake I am making not to get all the same data as the RAW version.

$data = DB::table('contractors')
    ->join('auburntree.crews', 'contractors.crew_id', '=', 'crews.id' )
    ->join('auburntree.skill_type', 'contractors.crew_id', '=', 'crews.id')
    ->join('auburntree.location', 'location.id', '=', 'crews.location_id' )
    ->select('contractors.crew_id count(*) as members, skill_type.skill, location.location_name, crews.crew_name')
    ->groupBy('contractors.crew_id')
    ->get();

        dd($data);

The result:

array:6 [▼
  0 => {#264 ▼
    +"as": "1"
  }
  1 => {#265 ▶}
  2 => {#266 ▶}
  3 => {#267 ▶}
  4 => {#287 ▶}
  5 => {#268 ▶}
]

Sorry for I am sure is a Newb mistake.

Many Thanks !!!

0 likes
11 replies
JarekTkaczyk's avatar
Level 53

@vincej You need selectRaw there

    ->selectRaw('contractors.crew_id, count(*) as members, skill_type.skill, location.location_name, crews.crew_name')
vincej's avatar
Level 15

@JarekTkaczyk

Jarek - you are truly brilliant !! Thank you !

The syntax I used was copied straight out of the docs. Curious that it does not work.

You might notice that the essential structure of this query is similar to the Team / Players query you helped with on Tues. Only in this case it is Crew / Contractors. It is the same query, just I used Team / Players to make it obvious to the forum what I needed.

Then I realised I needed more data that just members count in the same query: Location, Crew Name, Skills. So I have built off of your example. I am now going to have to go through the same exercise as before: parsing over the array. It should be easier now with a foreach.

Many Thanks Again !! Another award point ! :o)

1 like
toniperic's avatar

The syntax I used was copied straight out of the docs. Curious that it does not work.

@vincej can you point exactly where in the docs have you copied it from?

toniperic's avatar

@vincej all the relevant snippets from the docs I could see (relevant to your code) is the following snippet actually:

$users = DB::table('users')
    ->join('contacts', 'users.id', '=', 'contacts.user_id')
    ->join('orders', 'users.id', '=', 'orders.user_id')
    ->select('users.*', 'contacts.phone', 'orders.price')
    ->get();

Is that the one? If yes, the docs are valid, it's just that you made a small mistake.

When aliasing something (a sql function call) in your query statement (using select count(*) AS total) you should always stick to selectRaw().

For a moment you made me think that the docs were wrong. :)

JarekTkaczyk's avatar

@toniperic @vincej In fact aliasing is handled by the builder/grammar:

select('name as first_name') OK

but

select("concat(first_name, ' ', last_name) as name") wrong

That said, you need selectRaw whenever you want to use sql functions, aggregates etc

vincej's avatar
Level 15

@JarekTkaczyk

I am sorry to trouble you again especially so late at night.

It does not feel right to have the query sitting in my controller. So following your example from Tues I place it in the model an try to call it:

  $contractorsTest = Crew::with('contractorsTest')->get();

Model:

    public function contractorsTest()
    {
        return $this->hasOne('App\Models\Contractor')
       /* DB::table('contractors')*/
            ->join('auburntree.crews', 'contractors.crew_id', '=', 'crews.id' )
            ->join('auburntree.skill_type', 'contractors.crew_id', '=', 'crews.id')
            ->join('auburntree.location', 'location.id', '=', 'crews.location_id' )
            ->selectRaw('contractors.crew_id, count(*) as members, skill_type.skill, location.location_name, crews.crew_name')
            ->groupBy('contractors.crew_id')
            ->get();
    }

And I get an error:

Call to undefined method Illuminate\Database\Eloquent\Collection::addEagerConstraints()

Please or to participate in this conversation.