suhaskunte777
1 year ago
290
5
Laravel

Undefined offset: 0 in foreach loop

Posted 1 year ago by suhaskunte777

I am gettting issue in foreach loop where i am converting data into array. dd my all data is recieving but in forloop it not working. I am using Laravel 5.7

    "php": "^7.1.3",
    "barryvdh/laravel-debugbar": "^3.2",
    "fideloper/proxy": "^4.0",
    "laravel/framework": "5.7.*",
    "laravel/tinker": "^1.0",
    "laravelcollective/html": "^5.7",
    "sven/artisan-view": "^3.1",
    "way/generators": "^3.0"

hear is my code in controller '''

$node = Qnode::select('id', 'main_id')->with('datas')->get()->toArray(); foreach ($node as $item) {

//this code give me right result 
dd($item['datas'][0]['pivot']['qanswer_id']);

//but here i am getting Undefined Offset:0
        $data[$item['id']] = ['main_id' => $item['main_id'], 'data_id' => $item['datas'][0]['pivot']['qanswer_id']];
    }
    Auth::user()->qnodes()->sync($data);

'''

Please sign in or create an account to participate in this conversation.