Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Fluent add select()s in separate places?

//Earlier in the code, in each Model:
query = ModelName::select('table_name.*')

//Later in the code in a function in a Trait class that is always called

    if ($column == 'group_by')
    {
        $thing_query->groupBy($value);
        $thing_query->select(DB::raw('COUNT('.$value.') as count'));
    }

Is there a way to append or include a separate select function in the eloquent query builder?

The actual ->select() is set earlier and then this function is called. I'd like to add the count column conditionally in this later function that has the query passed into it.

like image 420
Damon Avatar asked Jun 24 '13 20:06

Damon


1 Answers

For future reference, you can use the addSelect() function.

It would be good to have in the documentation, but you'll find it here in the API: http://laravel.com/api/4.2/Illuminate/Database/Query/Builder.html#method_addSelect

like image 153
Cameron Avatar answered Nov 07 '22 12:11

Cameron