Reputation:
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.
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: []
}
Upvotes: 0
Views: 862
Reputation: 1
all() gives you an collection (similar to array), so it has index (0 and positive integers) for keys
use pluck() to get all specific value for a key
$notificationData = UserEnabledNotifications::pluck('id');
print($notificationData) // [1, 2, 3, 4, 5, 6, ...]
Upvotes: 0
Reputation: 6005
Try this
// 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);
OR
$notificationData = $notificationData->pluck('id')->toArray();
dd($notificationData);
Upvotes: 1
Reputation: 3035
The $notificationData
is an object of type Illuminate\Database\Eloquent\Collection, 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();
Upvotes: 2