Skip to content
Advertisement

Laravel fetch all the values of a column

The user_enabled_notifications table has 2 rows of data. i wanted to fetch all the values in the id column.

$notificationData = UserEnabledNotifications::all();

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

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

dump($notificationData[0]['id']); shows only 1 id. What else shall i try to fetch all the id column values in a single stretch.

However, dump($notificationData); shows the complete data in table as given below.

IlluminateDatabaseEloquentCollection {#337
  #items: array:4 [
    0 => AppModelsUserEnabledNotifications {#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 => AppModelsUserEnabledNotifications {#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: []
    }

Advertisement

Answer

The $notificationData is an object of type IlluminateDatabaseEloquentCollection, a collection. If you want to get the id of individual elements. Pls do the followings:

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

// Or
$notificationData->pluck('id')->toArray();
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement