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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:00:03  来源:igfitidea点击:

laravel eloquent Array to string conversion

phpmysqllaraveleloquenthelper

提问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

enter image description here

在此处输入图片说明

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 firstdirectly 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)