Friday, April 14, 2017

Undefined offset: 0 - How do I access array element - Laravel 5.4

I'm trying to mail a verification code to a user. In order to do that I've to declare a variable in array like this,

$data = array(
     'code' => rand(1000, 9999)
);

And then I want to save it to the DB:

$user->code = $data['code'];
$user->save();

But it gives an error:

undefined offset 0 or Creating default object from empty value.

What should I do?



via ishan shah

Advertisement