2017-09-04 24 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

这是国家编号 –