2017-07-19 69 views
0

我知道很簡單,但我不知道在這一刻不能使用類型爲stdClass的對象作爲數組laravel

在我的控制,我使用

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

我無法從獲得的數據進行該數組

dd($result)告訴我:

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

如何獲得約翰出來????

我已經試過$result[0]["name"]但沒有成功,給我的錯誤

+4

'$結果[0] - > name' –

+0

簡單而有效:d THX – calin24

回答

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

然後:

$result->name; // John 

或者,你可以這樣做:

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

然後:

$result; // John 
+0

不與'GET'陣列(或嚴格說,這一結果 - 集合)?你不應該用'first()'來代替嗎? –

+0

它工作正常:D謝謝你$結果= DB ::表('帳戶名') - >其中('名稱',$名稱) - >價值('名稱'); – calin24

+0

是的第一次返回一個集合;) – calin24

0

您可以將對象投射到像這樣的數組(array) $object
您還可以檢查它是否是一個對象:if(is_object($variable)) $variable = (array) $variable

相關問題