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?
 
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
 
Chris StanyonCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.