简体   繁体   English

无法将类型为stdClass的对象用作数组laravel

[英]Cannot use object of type stdClass as array laravel

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: dd($result)显示给我:

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 我试过$result[0]["name"]但没有成功,并给我那个错误

$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

You can cast object to array like that (array) $object . 您可以像这样将对象转换为数组(array) $object
You can also check if it's an object : if(is_object($variable)) $variable = (array) $variable 您还可以检查它是否是一个对象: if(is_object($variable)) $variable = (array) $variable

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM