Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ggalinec's avatar

Selecting only a few columns from a table

I'm trying to fetch just a few columns, instead of all of them, from a table and I can't get it to work.

$products = Product::select('*')->publishedOrHidden()->with('Category');

So I have a Product model with a scope and a Category.

I'm logging all my MySQL queries and every time it's fetching all columns from the database ('*').

$products = Product::select(['product_id', 'status', 'name', 'price', 'currency_code', 'category_id'])->publishedOrHidden()->with('Category');

or

$products = Product::select('product_id, status, name, price, currency_code, category_id')->publishedOrHidden()->with('Category');

I'm using select method on an Eloquent model as shown here -> http://datatables.yajrabox.com/eloquent/basic-object

0 likes
13 replies
tykus's avatar
Product::with('category')->publishedOrHidden()->get(['product_id, status, name, price, currency_code, category_id']);
tykus's avatar

That won't work @Ricardo - he wants specific columns, not a specific number of rows

Ricardo's avatar

@tykus_ikus that's why I delete my answer, I must read more carefully ;) (you were faster than me)

Snapey's avatar

what have you tried? You are showing select(*) in your example?

jekinney's avatar

In the get() method you can pass an array.

get(['column_1', 'etc']). First also. Find (id, ['colmon_1', 'etc']);

Also eager loading (with()) you want first not at the end. To select specific columns from a relationship look at the docs for advance where statements. Replace where for select.

ggalinec's avatar

I've tried the array and string inside of the select function but it's being ignored. The code by @tykus_ikus works but I loose all the magic of datatables (filters, search, pagination etc.)

tykus's avatar

So? The DataTables of method needs an instance of Builder rather than a Collection- which your second query will actually give you, but maybe DataTables doesn't like the nested data? Have you tried it without the with method appended?

Have a look at the Master-Detail example on the DataTables docs to see how related data is handled- it might suit you use case where you want to pull in the Category

veve286's avatar
veve286
Best Answer
Level 8

$products = Product::with('Category')->select(['product_id', 'status', 'name', 'price', 'currency_code', 'category_id'])->publishedOrHidden()->get();

bobbybouwmann's avatar

@ggalinec Remember that Laravel uses foreign keys for relations. This means that if for example the user_id is not in the query it won't be able to fetch the posts where that user_id is set, since it's not in the sql query!

ggalinec's avatar

It's checking the status of the product (enum - PUBLISHED, HIDDEN, DELETED) - it's fetching everything but the deleted ones.

ggalinec's avatar

This seems to work

$products = Product::with('Category')->select(['product_id', 'status', 'name', 'price', 'currency_code', 'currency_id', 'category_id']);

I've removed the scope and only kept the Category.

Please or to participate in this conversation.