【问题标题】:Get column all values in one array using Eloquent使用 Eloquent 获取一个数组中的所有值
【发布时间】:2016-03-10 12:28:28
【问题描述】:

我想在单个数组中获取特定列的所有值。寻找可以做到这一点的 Eloquent 函数。

类似这样的:

Model::select('id')->where('type', 'user')->asArray()

预期结果是:

[1,2,3,4,5,6,7,8,9]

【问题讨论】:

    标签: arrays select eloquent


    【解决方案1】:

    Eloquent 没有单独的内置函数来执行此操作。但是,您可以使用mapModel 对象的集合展平为一个数组:

    $coll = Model::select('id')->where('type', 'user')->get();
    
    // Pull the id out of each member of the collection
    $coll = $coll->map(function ($item, $key) {
        return $item->id;
    });
    
    // Convert collection to an array
    print_r($coll->toArray());
    

    【讨论】:

      猜你喜欢
      • 2020-04-24
      • 1970-01-01
      • 1970-01-01
      • 2014-10-03
      • 2017-10-13
      • 1970-01-01
      • 2020-01-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多