2017-09-04 75 views
1
$country = DB::table('location_countries')->where('name','India')->first(); 
$country_id = $country['_id']; 
echo $country_id; 
$states = DB::table('location_states')->where('country_id',$country_id)->first(); 
echo $states['name']; 

上面是我在控制器中運行的代碼,我使用jenseggers/mongodb作爲mongodb連接。傳遞到函數時Laravel變量值丟失

直到該部分的代碼我可以看到響應當我這樣做的回波

$country = DB::table('location_countries')->where('name','India')->first(); 
$country_id = $country['_id']; 
echo $country_id; 

我通過$country_id給函數如下它後顯示沒有響應

$states = DB::table('location_states')->where('country_id',$country_id)->first(); 
echo $states['name']; 

如果我直接傳遞字符串像下面的代碼,而不是傳遞$country_id變量然後我得到一個響應,但如果我傳遞一個變量沒有響應。

$states = DB::table('location_states')->where('country_id','value-here')->first(); 

爲什麼會發生這種情況以及如何繞過這種情況。

+0

嘗試$ states = DB :: table('location_states') - > where('country_id','=',$ country_id) - > first(); –

+0

之前嘗試過,但它沒有奏效。 –

+0

嘗試dd($ states);就在那條線的下方,看到o/p –

回答

2

查詢返回的對象不是一個數組所以使用這樣的:

如果你想獲取ID柱:$country->id;

這同樣適用於狀態,例如真命名柱:$states->name;

也可以嘗試用trim也許有任何空白空間:

where('country_id', trim($country_id)) 
+0

這也不起作用,因爲我以前曾嘗試過。 –

+0

你的'echo $ country_id;'是什麼? – C2486

+0

這是國家編號 –