我是 laravel 的新手,我运行查询并从数据库中获取行,我想在查看这些行之前编辑这些行的列。 所以这是我的代码片段:
$callPlans = CustomerCallPlan::whereNotNull('id');
foreach ($callPlans->get() as $callPlan) {
dd($callPlan);
}
以及输出截图:
我需要将 numbertemplate
列的所有“x”字符替换为“-”。
最佳答案
如果您想始终为您的模型执行此转换,您只需将以下访问器方法添加到模型类:
public function getNumbertemplateAttribute() {
return str_replace('x', '-', $this->attributes['numbertemplate']);
}
现在每次访问 $customerCallPlan->numbertemplate 时,您都会得到转换后的字符串。
否则只需在获取数据时转换列:
$plans = $callPlans->get()->map(function($plan) {
$plan->numbertemplate = str_replace('x', '-', $plan->numbertemplate);
return $plan;
});
关于php - 如何编辑 Laravel 集合中的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37586652/