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'];

above is the code i am running in the controller i have used jenseggers/mongodb for the mongodb connection.

till this part of code i can see the response when i do the echo

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

after i pass the $country_id to the function as below it shows no response

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

if i directly pass the string like the below code instead of passing the $country_id variable then i get a response but if i pass a variable there is no response.

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

Why is this happening and how to bypass this.

3
  • Try $states = DB::table('location_states')->where('country_id', '=' ,$country_id)->first(); Commented Sep 4, 2017 at 17:24
  • Tried this before but it didn't work. Commented Sep 4, 2017 at 17:26
  • Try dd($states); just below that line and see o/p Commented Sep 4, 2017 at 17:28

1 Answer 1

2

Query returns an object not an array so use it like this:

If you want to retrieve the id column: $country->id;

The same is true for states, e.g., the name column: $states->name;

Also try with trim maybe there is any blank space:

where('country_id', trim($country_id)) 
Sign up to request clarification or add additional context in comments.

7 Comments

This is also not working as i have tried this before.
What is your , echo $country_id; ?
it is echoing a value for country id
Also try with trim : where('country_id',trim($country_id))
worked perfectly with trim. Whats the logic behind 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.