0

I have a function which gets the value from a database and returns. When I echo, the value does exist. But returned value is null;

public static function getCountryCode($country) {       
    $country = (int) $country;      
    $x = Country::where('id', $country)->get();
    $country_code = '';         
    foreach($x as $row)             
        $country_code = $row->alpha_2;
    //return 'bd';      
    echo $country_code;         
    return $country_code; 
}

Not sure what's wrong in there. It's a laravel project.

Function that is calling this method

public function countryselect()
{
    $country_id = HomeController::detectCountry();
    $country_code = SiteController::getCountryCode($country_id);
    var_dump($country_code);
    return View::make('Layout.countryselect', compact('country_id', 'country_code'));
}
4
  • Can you show the code that calls this method? Commented Sep 20, 2014 at 7:38
  • public function countryselect() { $country_id = HomeController::detectCountry(); $country_code = SiteController::getCountryCode($country_id); var_dump($country_code); return View::make('Layout.countryselect', compact('country_id', 'country_code')); } Commented Sep 20, 2014 at 7:42
  • It'll be easier to format if you add it to the question. There's a pending edit there though that needs bringing in too. Commented Sep 20, 2014 at 7:44
  • this is function that is used to call @ bcmcfc Commented Sep 20, 2014 at 7:45

1 Answer 1

1

You don't have to use "foreach" function. You can get country code in much easier way. Just make some changes in getCountryCode() like

public static function getCountryCode($countryId) {
    $countryId = (int) $countryId;      
    $country = Country::where('id', $countryId)->get()->first();
    return $country->code;
}

This will return the country code of specified country id. Use descriptive name for variable like you used for the function

Sign up to request clarification or add additional context in comments.

1 Comment

thnx for letting me know this

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.