Shafiq Mustapa
Shafiq Mustapa

Reputation: 433

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

Upvotes: 1

Views: 16851

Answers (3)

Alexey Mezenin
Alexey Mezenin

Reputation: 163748

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

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

Upvotes: 5

Bara' ayyash
Bara' ayyash

Reputation: 1935

You need to loop through the returned result from Helper::getAddress(112233)

Upvotes: 0

Amit Gupta
Amit Gupta

Reputation: 17658

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();

Upvotes: 2

Related Questions