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?

推荐答案

Take note that only() will return item(s) with the specified key which is applied to an array with keys or associative array. As example given, it is array with keys

['product_id' => 1, 'name' => 'Desk', 'price' => 100, 'discount' => false]

但是,如果您测试将only()与来自Eloquent 结果的集合一起使用,其中包含集合/对象数组(无键).

[ object, object, object ]

如果集合中包含键,它将起作用.

[ 'product_id' => object, 'name' => object ]

因此,在您只过滤指定键的值的情况下,我建议使用pluck()map()

Laravel相关问答推荐

在Laravel Sail Docker环境中的PHPMyAdmin中出现`Out of Memory‘错误

Laravel FAKER语法

如何从@foreach 循环中捕获所有数据并将其传递给 值? - Laravel

Laravel 9 如何将 url 中的参数从一个控制器传递到另一个控制器?

使用 laravel 根据当月显示注册用户列表

如何在laravel中输出奇偶行

给定的角色或权限应该使用 guard `` 而不是 `web`. -Laravel

Laravel belongsTo 关系 - 试图获取非对象的属性

Laravel 控制器中一种特定方法的中间件

Laravel lockforupdate(悲观锁)

在 laravel 分页中添加一些数据

向 Docker 上的 Artisan 推荐方式

如何更改默认 Laravel Auth 登录视图

Laravel 5 Carbon 全局语言环境

Laravel 邮件密件抄送

如何在 laravel 用户删除中重置自动增量?

在 laravel 中安装 vue 3.0

如何在 Laravel 测试中禁用选定的中间件

如何在 Laravel 5 中验证 RESTful API?

限制自己重载外部 API 的速率