本文介绍了在laravel 5.4中将空值转换为空字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将使用过 array_walk_recursive
的空字符串转换为所有null,但是我还没有想要的东西,请帮助我找出我在这里做错的地方.
I want to convert all null with empty string I have used array_walk_recursive
but I haven't got what I want please help me to figure it out what I have done wrong here.
protected function setData($key, $value)
{
$this->data[$key] = $value;
array_walk_recursive($this->data, function (&$item, $key) {
$item = null === $item ? '' : $item;
});
return $this->data;
}
推荐答案
好吧,这只是雄辩的灯错误,我们总是在雄辩的对象中得到结果,这里我在 array_walk_recursive
内部传递雄辩的对象所以在传递之前,我需要像这样在laravel中使用-> toArray()
方法将雄辩的对象转换为数组.
well, that's just a lamp mistake in eloquent we always get result in eloquent object and here I'm passing eloquent object inside array_walk_recursive
so before passing I need to convert into array from eloquent object using ->toArray()
method in laravel like this.
内部用户控制器
$this->setData("friendList", $loadFriends->friends->toArray());
然后 array_walk_recursive
将起作用.
protected function setData($key, $value)
{
array_walk_recursive($value, function (&$item, $key) {
$item = null === $item ? '' : $item;
});
$this->data[$key] = $value;
return $this->data;
}
这篇关于在laravel 5.4中将空值转换为空字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!