1

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

3 Answers 3

5

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

->pluck('address')[0];
Sign up to request clarification or add additional context in comments.

12 Comments

yeah, i need to return as a string. but your solutions return Call to a member function first() on array
Use [0] The first() method will also work if you'll use Eloquent instead of Query Builder.
Well @Shafiq which laravel you are using. Because I checked in Laravel 5 dd($result); gives string output.
So the conclusion is that your code is correct, right ?
yes the solutions is correct. i'm using local db which is not updated. after some git push, the code work fine. thanks
|
2

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

2 Comments

error return Call to a member function first() on array
If it's an array then try with [0].
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.