From docs, I tested the following example using the method->only()
$collection = collect(['product_id' => 1, 'name' => 'Desk', 'price' => 100, 'discount' => false]);
$filtered = $collection->only(['product_id', 'name']);
$filtered->all();
and it really works.
However , when I apply this method to the collection that I get from the database (Model),
$myCollection = MyModel::orderBy('id','desc')->paginate(5);
$filtered=$myCollection->only(['id']);
dd(filtered);
The returned collection is empty!
Collection {
}
If I get the dd($myCollection);collection from the database, it is really filled with additions, attributes, etc. Data is displayed correctly in table view mode.
But if I apply methods ->only()or ->except()to $myCollection... the returned collection is empty.
For example, this is part of a collection where I want to show only an attribute id, or a few more, but not all:
LengthAwarePaginator {
0 => MyModel {
+incrementing: true
+timestamps: true
"id" => 3041
"date" => "2017-01-25"
"value1" => "12"
"value2" => "20"
"value3" => "22"
"value4" => "25"
"value5" => "46"
"value6" => "48"
"value7" => "50"
"value8" => "$60,000,000.00"
"created_at" => null
"updated_at" => null
]
->only(['id']), .
paginate, , , LengthAwarePaginator.
, :
$filtered = collect(['id'=>$myCollection->pluck(['id']),'Value1'=>$myCollection->pluck('value1')]);
dd($filtered);
, , , :
Collection {
"id" => Collection {
"value1" => Collection {
]
}
? ? ?