php – Fatal error: Call to a member function update() on null problem-ThrowExceptions

Exception or error:

I have a section on the site where used items parsing from Steam API. Every item has an attribute market_hash_name, classid, and i have virtual items on the site which have constant value market_hash_name and classid, and these values do not change.

Items that are in the Steam service are stored in the database, the virtual items on the site are not stored in the database, but generated via json on the fly.

The site has the function of transferring items from the Steam service and virtual items to each other. Items that are in Steam (and they are registered in the database) are transmitted without problems, but virtual items are not transmitted (because that don’t have in database), and response an error: Fatal error: Call to a member function update() on null line: 851

My code:

    foreach ($itemsInfo as $item) {
        if (isset($item['classid'])) {
            $itemDB = Item::find($item['id']);

            $itemDB->update([
                'steamid' => $user->steamid64,
                'status' => 0
            ]);
        }
    }

At 851 line i have $itemDB->update.

The problem arises because I try to transfer virtual items, they are not in the database and therefore I get this error, because in the items table there is no my value classid for virtual items. How can i correctly set the condition, for example if item has classid == test, than use another code;?

How to solve:

As mentionned in earlier comments, you should check the returned value of the find() method :

    foreach ($itemsInfo as $item) {
        if (isset($item['classid'])) {
            if($itemDB = Item::find($item['id'])) {

                $itemDB->update([
                    'steamid' => $user->steamid64,
                    'status' => 0
                ]);
            }
        }
    }

Leave a Reply

Your email address will not be published. Required fields are marked *