[英]How to access single value property of a collection in laravel 5
我的控制器中有一个通过控制器传递的变量:name是$ others
当我做一个foreach并打印时
@foreach($others as $other)
{{ $other }}
@endforeach
输出为:
[{"employeeID":"9125123981003","email":"admin@gaincafe.com","fullName":"Pranshu Jain"}] [{"employeeID":"45755757577","email":"yoyo@pranshu.com","fullName":"Pranshu Jain"}]
我要从中访问单个属性? 他们是收藏吗?
我尝试过这样
@foreach($others as $other)
{{ $other->email }}
@endforeach
这是我如何获得这个$others
:
public function show($id)
{
$this->data['leave_application'] = Attendance::find($id);
$date_applied_on = $this->data['leave_application']->date;
$emp_id = $this->data['leave_application']->employeeID;
$other_on_leave = Attendance::where('date', '=', $date_applied_on)->get();
$employees_on_leave = [];
foreach($other_on_leave as $others) {
if($emp_id != $others->employeeID) {
$emptyarray = Employee::where('employeeID', '=', $others->employeeID)->get(array('employeeID','email','fullName'));
$employees_on_leave[] = $emptyarray;
}
}
$this->data['others'] = $employees_on_leave;
return view('admin.leave_applications.show', $this->data);
}
但这行不通。 请帮助和指导一点。 我熟悉数组和对象。 我曾经和他们做得很好。 但这似乎并不复杂。
如果我做dd($ others)然后我得到
array:2 [▼
0 => Collection {#344 ▼
#items: array:1 [▼
0 => Employee {#345 ▼
#guarded: array:1 [▼
0 => "id"
]
#hidden: array:1 [▼
0 => "password"
]
#connection: null
#table: null
#primaryKey: "id"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: array:3 [▶]
#original: array:3 [▶]
#relations: []
#visible: []
#appends: []
#fillable: []
#dates: []
#dateFormat: null
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
+wasRecentlyCreated: false
}
]
}
1 => Collection {#346 ▼
#items: array:1 [▼
0 => Employee {#347 ▼
#guarded: array:1 [▶]
#hidden: array:1 [▶]
#connection: null
#table: null
#primaryKey: "id"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: array:3 [▶]
#original: array:3 [▶]
#relations: []
#visible: []
#appends: []
#fillable: []
#dates: []
#dateFormat: null
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
+wasRecentlyCreated: false
}
]
}
]
在这里尝试first()
而不是get()
:
$emptyarray = Employee::where('employeeID', '=', $others->employeeID)->first(array('employeeID','email','fullName'));
如果您仍想使用get()
,请执行以下操作:
foreach($other_on_leave as $others) {
if($emp_id != $others->employeeID) {
$emptyarray = Employee::where('employeeID', '=', $others->employeeID)->get(array('employeeID','email','fullName'));
foreach($emptyarray as $arr){
$employees_on_leave[] = $arr;
}
}
}
并更改此行,以便视图可以理解要遍历的内容:
return view('admin.leave_applications.show', ['others' => $this->data['others', 'leave_application' => $this->data['leave_application']]);
让我知道它是否有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.