Skip to content

Trying to use raw query in select #1123

Closed
@jbxonline

Description

@jbxonline

I've been going round in circles a bit here - hoping someone can help or tell me what I'm doing wrong!

This query works fine:

$query = \DB::connection('mongodb')
        ->collection('statistics')
        ->whereIn('vehicle.branch_id', $branchIds)
        ->select(['event', 'created_at', 'vehicle.vin'])
        ->get();

However, I want to use raw, in order to return a column with a different name, and also perform aggregations:

$query = \DB::connection('mongodb')
        ->collection('statistics')
        ->whereIn('vehicle.branch_id', $branchIds)
        ->select(['event', 'created_at', \DB::raw('vehicle.vin as vin'), \DB::raw('COUNT(*) as count')])
        ->get();

This always gives me an Illegal Offset type in Builder.php line 353, as it is expecting an array of column names, but it gets instances of Expression.

I've tried building this query a few different ways, but can't get past this issue.
Any ideas?

TIA, Jon

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions