2

I know is simple but i don't know at this moment

In my controller i use

$result = DB::select("SELECT name FROM account_name WHERE name='".$name."'"); 

I can't get the data out from that array

dd($result) show me:

array:1 [
  0 => {#232
    +"name": "John"
  }
]

How to get that John out????

I have tried $result[0]["name"] but no success and give me that error

2
  • 4
    $result[0]->name Commented Jul 19, 2017 at 19:21
  • simple and efficient :D thx Commented Jul 19, 2017 at 19:23

2 Answers 2

2
$result = DB::table('account_name')->select('name')->where('name', $name)->first();

Then:

$result->name; // John

Or you can do:

$result = DB::table('account_name')->where('name', $name)->value('name');

Then:

$result; // John
Sign up to request clarification or add additional context in comments.

2 Comments

Aren't the results with get an array (or strictly said - collection)? Shouldn't you use first() instead?
it is working ok :D thank you $result = DB::table('account_name')->where('name', $name)->value('name');
1

You can cast object to array like that (array) $object.
You can also check if it's an object : if(is_object($variable)) $variable = (array) $variable

1 Comment

plus points for type casting. i often find myself completely forgetting type casting even exists because php doesn't require it.

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.