Search code examples
phpmysqllaraveleloquenthelper

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) !!} but getting an error of Array to string conversion.

Here is if the result of dd

enter image description here

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


Solution

  • You need to get first result from an array, so use this:

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