TobiasS's avatar
Level 10

Eloquent vs RAW differs in handling camelCase

Hi!

Just struggled with why there is a differ in camelCase when aliasing using eloquent or RAW expression. Differs in how camelCase of resourceId is return. Notice the difference in i vs I

Eg. RAW

$raw = DB::raw("SELECT
                periods.id,
                event_id AS resourceId
       FROM
                periods
				 JOIN events ON periods.event_id = events.id")->
       getValue(DB::connection()->getQueryGrammar());
$raw = collect(DB::select($raw));
dd($raw)

Returns, lowercase i, resourceid

0 => {#2256 ▼
      +"id": 1
      +"resourceid": 1

But eloquent

$periods = Period::whereIn('event_id', $events_id)
                        ->join('events', 'periods.event_id', '=', 'events.id')
                        ->get(['periods.id', 'user_id AS resourceId',

Returns camelCase, resourceId

array:6 [▼
        "id" => 85
        "resourceId" => 9
```


It works when I use eloquent, so there is no "real" problem, but I'm curious why it differs.

Thx in advance!
0 likes
3 replies
LaryAI's avatar
Level 58

The difference in the casing of the column name "resourceId" between the RAW query and the Eloquent query is due to the way Laravel handles column aliases.

In the RAW query, the column alias is not recognized by Laravel, so it returns the column name in lowercase. This is the default behavior of the database when no alias is provided.

In the Eloquent query, Laravel recognizes the column alias and converts it to camelCase by default. This is a feature of Laravel's Eloquent ORM.

If you want to achieve consistent casing for column aliases in both RAW and Eloquent queries, you can use the DB::connection()->raw() method to specify the column alias explicitly in the RAW query. Here's an example:

$raw = DB::raw("SELECT
                periods.id,
                event_id AS resourceId
       FROM
                periods
				 JOIN events ON periods.event_id = events.id AS resourceId");

$raw = collect(DB::select($raw));
dd($raw);

By providing the column alias explicitly in the RAW query, Laravel will return the column name with the specified casing.

Alternatively, if you want to change the casing of the column alias in the Eloquent query, you can use the selectRaw() method instead of get() and specify the column alias with the desired casing. Here's an example:

$periods = Period::whereIn('event_id', $events_id)
                ->join('events', 'periods.event_id', '=', 'events.id')
                ->selectRaw('periods.id, user_id AS resourceId')
                ->get();

dd($periods);

By using selectRaw(), you can specify the column alias with the desired casing, and Laravel will return the column name accordingly.

jlrdw's avatar

An alias has to be included in a select:

->selectRaw('count(dc_pets.petid) as CountOfpetid')

CountOfpetid is selected.

Just a reminder, there is really no eloquent, it's a shortcut language. It's converted to normal regular PDO / SQL in the framework.

Snapey's avatar

sorry I cant offer any advice except to say that the other two replies clearly did not read the question

Please or to participate in this conversation.