【问题标题】:Laravel fetch all the values of a columnLaravel 获取列的所有值
【发布时间】: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: []
    }

【问题讨论】:

    标签: php laravel postgresql eloquent laravel-8


    【解决方案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();
        

        【讨论】:

          猜你喜欢
          • 2017-06-18
          • 1970-01-01
          • 1970-01-01
          • 2019-04-26
          • 2021-03-01
          • 2018-06-29
          • 2021-04-07
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多