我是 Laravel 的新手,我运行一个查询并从数据库中获取行,我想在查看这些行之前编辑这些行的列。
这是我的代码片段:
$callPlans = CustomerCallPlan::whereNotNull('id');
foreach ($callPlans->get() as $callPlan) {
dd($callPlan);
}
以及输出截图:
我需要将所有“x”字符替换为“-”numbertemplate
柱子..
如果您想始终为模型执行此转换,则只需将以下访问器方法添加到模型类中即可:
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;
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)