azngunit81
azngunit81

Reputation: 1604

Laravel Eloquent specific columns error

I have the following in Laravel 4.x using eloquent:

Book table has a dozen fields.

$single_data = Book::
            selectRaw("GROUP_CONCAT(DISTINCT CAST(id as char) SEPARATOR ',') as type_id_list")
            ->first();

Question: the data returned is everything within the table inside Book AND type_id_list as seen from the selectRaw.

I would like to ONLY return what I specified within the selectRaw.

It was suggested that you get add an array of columns within first() or get() in order to retrieve this only - however it is not working with custom selectRaw where you specify an expression or your own wording. It throws an error and when the query is analyzed the array that you put in first/get gets appended as part of the select.

Does anyone have a work around?

Upvotes: 0

Views: 423

Answers (1)

Jarek Tkaczyk
Jarek Tkaczyk

Reputation: 81137

first() will return only that particular column but wrapped in a Book object anyway, so use pluck() instead:

$single_data = Book::
        selectRaw("GROUP_CONCAT(DISTINCT CAST(id as char) SEPARATOR ',') as type_id_list")
          ->pluck('type_id_list');

Upvotes: 1

Related Questions