Product::with('category')->publishedOrHidden()->get(['product_id, status, name, price, currency_code, category_id']);
Feb 9, 2016
13
Level 9
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
Level 8
$products = Product::with('Category')->select(['product_id', 'status', 'name', 'price', 'currency_code', 'category_id'])->publishedOrHidden()->get();
Please or to participate in this conversation.