我在表中有这些列:
- 编号
- 姓名
- 状态
- 输入
状态应为0
或1
。
- 0 表示不活跃
- 1 表示不活跃。
我想在我的商品列表中添加 status_name
字段。这可能吗?
$items = Item::where('type', 'test');
if(item.active == 1)
{
// add new column status_name="active"
}
else
{
// add new column status_name="inactive"
}
$items->get();
我不想使用循环。有没有办法只用这个查询而不使用循环来做到这一点。
最佳答案
您需要遍历项目集合并应用您的条件,例如:
$items = Item::where('type', 'test')->get();
foreach($items as $item){
$item->status_name = $port->active===1?'active':'inactive';
}
关于php - 根据 laravel 中现有列中的值添加新列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55413107/