php - 如何在 Laravel 7 导出中获取变量值?
问题描述
我正在尝试在 Status 列中获取 $status 值,所以我将如何使用下面的代码来获取它
public function collection() {
if(Auth::user()->is_accepted == 1) {
$status = 'Accepted';
} elseif (Auth::user()->is_accepted == NULL) {
$status = 'Waiting Confirmation';
} else {
$status = 'Rejected';
}
return Entrepreneur::select('name', 'contact', 'address', 'business_name', 'business_contact', 'business_address', 'is_accepted')->get();
}
public function headings(): array {
return [
'Name',
'Contact',
'Address',
'Business Name',
'Business Contact',
'Business address',
'Status'
];
}
解决方案
使用map()
函数参考链接https://laravel.com/docs/7.x/collections#method-map
通过这个你可以向任何集合添加新键
public function collection()
{
$entrepreneur = Entrepreneur::select('name', 'contact', 'address', 'business_name', 'business_contact', 'business_address')->get();
$entrepreneur->map(function ($row) {
if ($row['is_accepted'] == 1) {
$status = 'Accepted';
} elseif ($row['is_accepted'] == NULL) {
$status = 'Waiting Confirmation';
} else {
$status = 'Rejected';
}
return $status;
});
return $entrepreneur;
}
推荐阅读
- javascript - 在 Vue 中使用 lodash 油门时传递这个
- php - OPENDIR 来自 $_REQUEST 的值
- azure - IotHubClientTransientException:发生瞬态错误,请重试
- java - 使用 StandardXYToolTipGenerator 格式化工具提示值
- javascript - createStore 不会从初始状态更改为 Action 证明值
- cordova - 有没有办法使用 Ionic 3 在浏览器中捕获音频
- spring - 在 Spring AOP 中使用 Aspects 条件执行方法
- php - htmlentities 无法将 HTML 实体转换为字符
- cryptography - 无法创建名称为“http://www.w3.org/2000/09/xmldsig#hmac-sha1”的算法
- c# - Activator.CreateInstance 期间的 MissingMethodException