检查从 eloquent 返回的特定值 Laravel 8
Check a specific Value that returned from eloquent in Laravel 8
在我的 Laravel 控制器中,我有以下代码:
public function test($id)
{
$emp_check = logentry::where([
['EmployeeFK', '=', $id],
['DateTime', '>', Carbon::now()->subHours(8)]
])->latest()->get();
return $emp_check;
}
这些是从数据库返回的
{
"LogID": 2,
"DateTime": "2021-09-28 08:16:44",
"LocationX": null,
"LocationY": null,
"EmployeeFK": 1,
"Status": "checked out",
"ErrorResponse": null,
"SalaryChingValue": null,
"SalaryNote": null,
"LogNote": null,
"created_at": "2021-09-28 11:16:44"
}
有什么方法可以检查从 eloquent 结果返回的“状态”值吗?
我知道有一些具体的方法可以做到这一点,但我真的不知道怎么写。
您可以在 return 之前简单地执行以下操作:
if($emp_check['Status'] == 'your condition'){
//do something
}
不确定这是否是您需要的,如果不是,请更新您的问题。
可能是我理解错了,不过试试
$emp_check = logentry::where('EmployeeFK', $id)
->where('DateTime', '>', Carbon::now()->subHours(8))
->where('Status', $status)
->latest()->get();
或将其中一种状态更改为
$emp_check = logentry::where('EmployeeFK', $id)
->where('DateTime', '>', Carbon::now()->subHours(8))
->whereIn('Status', $statuses)
->latest()->get();
因为您正在使用 get()
方法来查找结果。 get()
方法将 return 一个可以循环的记录数组。您在 where condition 中使用 id,所以如果您总是获得一条记录,您可以使用:
if($emp_check[0]->Status == 'your status'){
//do something
}
最好使用first()
获取单条记录。
或者你可以使用 foreach loop
if($emp_check){
foreach($emp_check as $v){
if($v->Status == 'your status'){
//do something
}
}
}
在我的 Laravel 控制器中,我有以下代码:
public function test($id)
{
$emp_check = logentry::where([
['EmployeeFK', '=', $id],
['DateTime', '>', Carbon::now()->subHours(8)]
])->latest()->get();
return $emp_check;
}
这些是从数据库返回的
{
"LogID": 2,
"DateTime": "2021-09-28 08:16:44",
"LocationX": null,
"LocationY": null,
"EmployeeFK": 1,
"Status": "checked out",
"ErrorResponse": null,
"SalaryChingValue": null,
"SalaryNote": null,
"LogNote": null,
"created_at": "2021-09-28 11:16:44"
}
有什么方法可以检查从 eloquent 结果返回的“状态”值吗?
我知道有一些具体的方法可以做到这一点,但我真的不知道怎么写。
您可以在 return 之前简单地执行以下操作:
if($emp_check['Status'] == 'your condition'){
//do something
}
不确定这是否是您需要的,如果不是,请更新您的问题。
可能是我理解错了,不过试试
$emp_check = logentry::where('EmployeeFK', $id)
->where('DateTime', '>', Carbon::now()->subHours(8))
->where('Status', $status)
->latest()->get();
或将其中一种状态更改为
$emp_check = logentry::where('EmployeeFK', $id)
->where('DateTime', '>', Carbon::now()->subHours(8))
->whereIn('Status', $statuses)
->latest()->get();
因为您正在使用 get()
方法来查找结果。 get()
方法将 return 一个可以循环的记录数组。您在 where condition 中使用 id,所以如果您总是获得一条记录,您可以使用:
if($emp_check[0]->Status == 'your status'){
//do something
}
最好使用first()
获取单条记录。
或者你可以使用 foreach loop
if($emp_check){
foreach($emp_check as $v){
if($v->Status == 'your status'){
//do something
}
}
}