Laravel 获取列的所有值

Posted

技术标签:

【中文标题】Laravel 获取列的所有值【英文标题】:Laravel fetch all the values of a column 【发布时间】:2021-10-29 17:32:03 【问题描述】:

user_enabled_notifications 表有 2 行数据。我想获取id 列中的所有值。

$notificationData = UserEnabledNotifications::all();

dump($notificationData['id']); 显示Undefined index: id

dump($notificationData->id); 显示Property [id] does not exist on this collection instance

dump($notificationData[0]['id']); 仅显示 1 个 ID。我还应该尝试一次获取所有id 列值。

但是,dump($notificationData); 在下表中显示了完整的数据。

Illuminate\Database\Eloquent\Collection #337
  #items: array:4 [
    0 => App\Models\UserEnabledNotifications #338
      #table: "user_enabled_notifications"
      #fillable: array:3 [
        0 => "userId"
        1 => "notificationTypesId"
        2 => "status"
      ]
      #connection: "pgsql"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      +preventsLazyLoading: false
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:7 [
        "id" => 1
        "userId" => 1
        "notificationTypesId" => 1
        "status" => true
        "deleted_at" => null
        "created_at" => null
        "updated_at" => null
      ]
      #original: array:7 [
        "id" => 1
        "userId" => 1
        "notificationTypesId" => 1
        "status" => true
        "deleted_at" => null
        "created_at" => null
        "updated_at" => null
      ]
      #changes: []
      #casts: array:1 [
        "deleted_at" => "datetime"
      ]
      #classCastCache: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: []
      #touches: []
      +timestamps: true
      #hidden: []
      #visible: []
      #guarded: array:1 [
        0 => "*"
      ]
      #forceDeleting: false
      #enableLoggingModelsEvents: true
      #oldAttributes: []
    
    1 => App\Models\UserEnabledNotifications #339
      #table: "user_enabled_notifications"
      #fillable: array:3 [
        0 => "userId"
        1 => "notificationTypesId"
        2 => "status"
      ]
      #connection: "pgsql"
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      +preventsLazyLoading: false
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:7 [
        "id" => 2
        "userId" => 1
        "notificationTypesId" => 2
        "status" => true
        "deleted_at" => null
        "created_at" => null
        "updated_at" => null
      ]
      #original: array:7 [
        "id" => 2
        "userId" => 1
        "notificationTypesId" => 2
        "status" => true
        "deleted_at" => null
        "created_at" => null
        "updated_at" => null
      ]
      #changes: []
      #casts: array:1 [
        "deleted_at" => "datetime"
      ]
      #classCastCache: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: []
      #touches: []
      +timestamps: true
      #hidden: []
      #visible: []
      #guarded: array:1 [
        0 => "*"
      ]
      #forceDeleting: false
      #enableLoggingModelsEvents: true
      #oldAttributes: []
    

【问题讨论】:

【参考方案1】:

all() 为您提供一个集合(类似于数组),因此它具有键的索引(0 和正整数)

使用 pluck() 获取键的所有特定值

$notificationData = UserEnabledNotifications::pluck('id');

print($notificationData) // [1, 2, 3, 4, 5, 6, ...]

【讨论】:

【参考方案2】:

试试这个

// get your main collection with all the attributes...
$notificationData = UserEnabledNotifications::get();

// build your second collection with a subset of attributes. this new
// collection will be a collection of plain arrays, not UserEnabledNotifications models.
$subset = $notificationData->map(function ($notification) 
    return collect($notification->toArray())
        ->only(['id'])
        ->all();
);
dd($subset);

$notificationData = $notificationData->pluck('id')->toArray();
dd($notificationData);

【讨论】:

【参考方案3】:

$notificationData 是一个 Illuminate\Database\Eloquent\Collection 类型的对象,一个集合。如果要获取单个元素的 id。请执行以下操作:

foreach ($notificationData as $notification) 
  dump($notification->id);


// Or
$notificationData->pluck('id')->toArray();

【讨论】:

以上是关于Laravel 获取列的所有值的主要内容,如果未能解决你的问题,请参考以下文章

Laravel Eloquent 搜索 JSON 数组以获取特定列的值

Laravel:迁移更改列的默认值(布尔值)

laravel 5 - 获取相关模型值

Laravel:按列值检索记录

在laravel中以雄辩的关系获取错误列的数据

Laravel 多表连接:如何连接两个包含所有列的表,但一列除外