chern123's avatar

Retrieve only specific columns from find() method

Intuitively, I would think that if you would like to retrieve only specific values from a model that is retrieved in Eloquent using the find method, it would look something like this:

        return Auth::user()->some_model()->find($id)->get(['col_a', 'col_b']);

What it seems to do, however, is return ALL models in the database with 'col_a', 'col_b' values for each of the models (associated with that user in particular).

How would one only get the model returned by the find($) and then grab those two columns? Worth noting this is in Laravel 5.4

1 like
2 replies
chern123's avatar

Found this shortly after posting... should have looked a little bit longer.


        return Auth::user()->some_model()->find($id, ['col_a', 'col_b']);

Apparently the answer lies in adding the columns to the find function.

1 like
Cronix's avatar

alternatively you can also ->select('col_a', 'col_b')->find($id)

2 likes

Please or to participate in this conversation.