laravel 5 - Trying to get property of non-object

Can anyone help me work this out. I am calling a name spaced method and I am getting an error on line 25 which is this link

if ($country->countryCode == NULL || $country->countryCode == '-') {

Open in new window


the error is:
at HandleExceptions->handleError('8', 'Trying to get property of non-object', '/home/forge/default/releases/20150407094225/app/Apollo/IP/Location.php', '25', array('ip' => '222.154.228.27', 'ipnumber' => '3734692891', 'country' => null)) in Location.php line 25

The whole Location.php class is so simple and below:

<?php
/**
 * Created by PhpStorm.
 * User: Air
 * Date: 18/12/14
 * Time: 6:11 PM
 */

namespace App\Apollo\Ip;


class Location {

    protected $ip;

    // get the country code based on the users ip address
    public static function getCountry() {
        // grab user ip
        $ip = \Request::getClientIp();
        // convert to ip number
        $ipnumber = ip2long($ip);
        // query DB to get country ID
        $country = \DB::table('ip2location')->select('countryCode')->whereRaw('? <= ipTo', array($ipnumber))->first();
        // if the country value returned is not null and - then add a default of the US
        if ($country->countryCode == NULL || $country->countryCode == '-') {
            $country->countryCode = 'US';
        }
        return $country->countryCode;
    }

}

Open in new window


I am calling it in side another class using like so:
$country = \App\Apollo\Ip\Location::getCountry();

Open in new window


If I run it through tinker I get s stdClass but when I run it inside the script I get:

at HandleExceptions->handleError('8', 'Trying to get property of non-object', '/home/forge/default/releases/20150407094225/app/Apollo/IP/Location.php', '25', array('ip' => '222.154.228.27', 'ipnumber' => '3734692891', 'country' => null)) in Location.php line 25

Do not understand and would really like some help. Because this script used the ip address of the user I have to deploy it to test it and it is taking me forever to change deploy test and still the same error.

Thanks for your help
LightwalkerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
I think it is possible that $country is a collection of some kind - perhaps an array encapsulating the one row from the db::table()->first() method.  You might use var_dump() to print out the value.  I know that tinker should give you insight here, but in my experience Laravel has a lot of "interesting" internals that may not be readily transparent.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Chris StanyonWebDevCommented:
Looking at the error, your line 23 is returning null (the database query), so $country = null. You can't get the properties of null - it's not an object.

Check your query is right, and maybe add in some error checking to handle a null return from your database.
0
LightwalkerAuthor Commented:
Thanks for your help, your where right, it turns out that my DB table was empty as the transfer had not taken. Always the simplest answer.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.