简体   繁体   中英

Laravel Trying to get property 'name' of non-object. Where problem?

I try to save in database information, which i received via API.

I try to save name , ratings and platforms in database, which comes in results array. Ratings received in array, platforms , how i understand received in double arrays.

I try to make something like this:

    $response = \Rawg::load('games')->setParams([
        'page' => 1,
        'page_size' => 40,
        'ordering' => '-rating',
    ])->get();

    foreach ($response as $result) {
        Games::insert(
            ['name' => $result->name, 'ratings' => $result->ratings->title, 'platforms' => $result->platforms->platform['name']]
        );
    }

But i received error Trying to get property 'name' of non-object . Where can be my problem? I'll be glad to get help.

This is an array not an object, and needs to change $response :

foreach ($response['results'] as $result) {
    Games::insert([
        'name' => $result['name'], 
        'ratings' => $result['ratings'][0]['title'], 
        'platforms' => $result['platforms'][0]['platform']['name']
    ]);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM