From the docs, I have tested the following example with the ->only()
method
$collection = collect(['product_id' => 1, 'name' => 'Desk', 'price' => 100, 'discount' => false]);
$filtered = $collection->only(['product_id', 'name']);
$filtered->all();
// ['product_id' => 1, 'name' => 'Desk']
and it is working indeed.
However, when I apply that method to a collection I get from the database (Model),
$myCollection = MyModel::orderBy('id','desc')->paginate(5);
$filtered=$myCollection->only(['id']);
dd(filtered);
返回的集合为空!
Collection {#199 ▼
#items: []
}
If I dd($myCollection);
the collection gotten from the database, it is indeed full of the appends, attributes, and so on stuff. The data appears correctly in the table blade view.
但如果我用->only()
或->except()
到$myCollection
的方法...返回的集合为空.
For example, this is the piece of the collection, where I only want to show the id
attribute, for example, or some more but not all:
LengthAwarePaginator {#218 ▼
#total: 3041
#lastPage: 609
#items: Collection {#219 ▼
#items: array:5 [▼
0 => MyModel {#220 ▼
#dates: array:1 [▶]
#appends: array:5 [▶]
#connection: null
#table: null
#primaryKey: "id"
#keyType: "int"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: array:12 [▼
"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'])
时,集合返回为空.
I have tested it without the paginate and the problem of the empty collection is still the same, so I don't think it has to do with the LengthAwarePaginator
.
Workaround Unfortunately I am achieving this the hard way:
$filtered = collect(['id'=>$myCollection->pluck(['id']),'Value1'=>$myCollection->pluck('value1')]);
dd($filtered);
Now I am getting the desired collection where, for example I just want two attributes or columns from the database:
Collection {#212 ▼
#items: array:2 [▼
"id" => Collection {#201 ▶}
"value1" => Collection {#211 ▶}
]
}
Why is this happening? What am I missing? How can I fix it?