In laravel 5.4, I'm able to retrieve fillable fields by using fillable
index of model instance.
$model = new AnyClass();
dd($model['fillable']);
The above code prints all fillable fields of AnyClass
. But the same code prints null
on laravel 5.6. I know I can retrieve fillable fields using $model->getFillable()
. My question is what is the reason / why it is not working in laravel 5.6 but works in 5.4?
If you look at Laravel's source code you'll see the difference.
The Model class, which is extended by the application models, implements the ArrayAccess
interface, which, among others, force the class to define the offsetGet
method.
In Laravel 5.4 the offsetGet
method looks like:
public function offsetGet($offset)
{
return $this->$offset;
}
which means that if you call $model['fillable']
, you actually call $model->offsetGet('fillable')
which actually returns the fillable
property of the class.
I couldn't find the Laravel 5.6 tag but I'm pretty sure it is the same code as Laravel 5.5.45. In this version the offsetGet
method was changed to:
public function offsetGet($offset)
{
return $this->getAttribute($offset);
}
which means that it actually returns the attribute if found or null otherwise.