laravel eloquent 数组到字符串的转换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41322090/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
laravel eloquent Array to string conversion
提问by Shafiq Mustapa
I building a helper in laravel to return a single field from eloquent.
我在 laravel 中构建了一个助手以从 eloquent 返回单个字段。
class Helper {
public static function getAddress($id) {
//intval($id);
$result = DB::table('tableaddress')
->where('id',$id)
->pluck('address');
//dd($result);
return $result;
}
}
In the view i'm calling it via {!! Helper::getAddress(112233) !!}
but getting an error of Array to string conversion
.
在视图中,我通过调用它{!! Helper::getAddress(112233) !!}
但收到Array to string conversion
.
Here is if the result of dd
这是如果结果 dd
How do I get the address to return a string. Thanks
如何获取返回字符串的地址。谢谢
回答by Alexey Mezenin
You need to get first result from an array, so use this:
您需要从数组中获取第一个结果,因此请使用以下命令:
->pluck('address')[0];
回答by Amit Gupta
You can try it as:
您可以尝试如下:
{!! Helper::getAddress(112233)->first() !!}
Or add first
directly in your helper function as:
或者first
直接在你的辅助函数中添加:
$result = DB::table('tableaddress')
->where('id',$id)
->pluck('address')
->first();
回答by Bara' ayyash
You need to loop through the returned result from Helper::getAddress(112233)
您需要遍历从返回的结果 Helper::getAddress(112233)