Laravel Fluent add select()s in separate places?

Damon picture Damon · Jun 24, 2013 · Viewed 11.1k times · Source
//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.

Answer

Cameron picture Cameron · Feb 10, 2014

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