简体   繁体   中英

laravel eloquent Array to string conversion

I building a helper in laravel to return a single field from 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) !!} {!! Helper::getAddress(112233) !!} but getting an error of Array to string conversion .

Here is if the result of dd

在此处输入图片说明

How do I get the address to return a string. Thanks

您需要从数组中获取第一个结果,因此请使用以下命令:

->pluck('address')[0];

You can try it as:

{!! Helper::getAddress(112233)->first() !!}

Or add first directly in your helper function as:

$result = DB::table('tableaddress')
           ->where('id',$id)
           ->pluck('address')
           ->first();

您需要遍历Helper::getAddress(112233)返回的结果

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM