2

I am currently trying to return a certain value from my database call in my controller with laravel. My controller currently looks like this:

public function systemView(Request $request, $id){
        $rpSystemCreatedBy = DB::table('rp_systems')->join('users', 'users.id', '=', 'rp_systems.user_id_creator')->select('users.name')->get();
        return $rpSystemCreatedBy;
    }

The return value returns:

[{"name":"Dan Marks"}]

I want to return just:

Dan Marks

However I have tried multiple different way and I keep getting an error that ["name"] is invalid. Ive used a var dump and i get this:

array(1) { [0]=> object(stdClass)#180 (1) { ["name"]=> string(9) "Dan Marks" } }

I am now stuck and do not know how to continue. Thank you for any help.

1
  • If you return an array in your controller, it will be converted to json automatically. That's why you should return a string instead. Commented Mar 10, 2016 at 14:33

1 Answer 1

2

Use this to get name:

$rpSystemCreatedBy[0]->name

It's just an example which will return name of the first row. To get all names in your template, use @foreach:

@foreach ($rpSystemCreatedBy as $a)
    {{ $a->name }}
@endforeach

Or in controller:

foreach ($rpSystemCreatedBy as $a){
    $theName = $a->name;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much I been ripping my hair out over this and its such a simple thing.

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.