Reputation: 433
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
How do I get the address to return a string. Thanks
Upvotes: 1
Views: 16851
Reputation: 163748
You need to get first result from an array, so use this:
->pluck('address')[0];
Upvotes: 5
Reputation: 1935
You need to loop through the returned result from Helper::getAddress(112233)
Upvotes: 0
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