我想从practice表访问name属性,但我正在获取集合。我的代码看起来像。
$practice = Practice::withTrashed()->where('id',$appointment->practice_id)->get();
dd($practice);
结果看起来像
Collection {#697
#items: array:1 [
0 => Practice {#698
#dates: array:1 [
0 => "deleted_at"
]
#fillable: array:2 [
0 => "name"
1 => "email"
]
#connection: null
#table: null
#primaryKey: "id"
#keyType: "int"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: array:7 [
"id" => 42
"name" => "DJ and Charlie Eye Associates"
"email" => "vendorsupport@ocuhub.com"
"created_at" => "0000-00-00 00:00:00"
"updated_at" => "2017-01-13 06:29:14"
"liferay_id" => 57238
"deleted_at" => "2017-01-13 06:29:14"
]
#original: array:7 [
"id" => 42
"name" => "DJ and Charlie Eye Associates"
"email" => "vendorsupport@ocuhub.com"
"created_at" => "0000-00-00 00:00:00"
"updated_at" => "2017-01-13 06:29:14"
"liferay_id" => 57238
"deleted_at" => "2017-01-13 06:29:14"
]
#relations: []
#hidden: []
#visible: []
#appends: []
#guarded: array:1 [
0 => "*"
]
#dateFormat: null
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
+wasRecentlyCreated: false
#forceDeleting: false
}
]
}
有什么办法吗,这样我就可以得到“DJ和查理眼协会”的名字。我使用了thrashed方法,因为这个条目在practice表中是软删除的。
最佳答案
首先需要从集合中获取对象,然后可以获取该对象的任何属性:
$practice->first()->name;
或:
$practice[0]->name;