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

Time:02-10

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.

CodePudding user response:

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']
    ]);
}
  •  Tags:  
  • Related