【发布时间】:2021-10-17 06:46:09
【问题描述】:
$notification=Notifications::where('status','false')->get();
$data=$notification->all();
//dd($data);
dd($data['notificationTypeName']);
在上面的代码中,Notifications 是模型名称。尝试访问数组$data 的列notificationTypeName 时出现错误Undefined index: notificationTypeName。
dd($data) 给出如下输出:
array:4 [
0 => App\Models\Notifications {#323
#table: "notifications"
#fillable: array:6 [
0 => "notificationTypesId"
1 => "notificationTypeName"
2 => "userId"
3 => "email"
4 => "status"
5 => "recipientName"
]
#connection: "pgsql"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
+preventsLazyLoading: false
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:9 [
"id" => 5
"notificationTypesId" => 3
"notificationTypeName" => "Cart Expired"
"userId" => 1
"email" => "me.g@k.com"
"recipientName" => "John"
"status" => false
"created_at" => null
"updated_at" => null
]
#original: array:9 [
"id" => 5
"notificationTypesId" => 3
"notificationTypeName" => "Cart Expired"
"userId" => 1
"email" => "me.g@k.com"
"recipientName" => "John"
"status" => false
"created_at" => null
"updated_at" => null
]
#changes: []
#casts: []
#classCastCache: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#guarded: array:1 [
0 => "*"
]
#enableLoggingModelsEvents: true
#oldAttributes: []
}
【问题讨论】:
-
使用 foreach 从对象中获取数据
-
@RushikeshGanesh 我需要在不使用 foreach 的情况下获取列,因为每次获取时我都需要输出中来自 db 的所有行。这就是我面临的问题。
-
试试
dd($data[0]['notificationTypeName']); -
在 $notification 中有多少条记录是单条还是多条
标签: php laravel postgresql eloquent laravel-8