I think my brain isn't functioning today as I can't seem to get my head around this one.
I have a class that has a data array, for example -
class Testing {
protected $fillable = ['questions.*.checked'];
protected $data = [
'active' => true,
'questions' => [
[
'question' => 'This is the first question',
'checked' => true,
],
[
'question' => 'This is the second question',
'checked' => false,
]
]
];
public function fill(array $attributes = []) {
// take our attributes array, check if the key exists in
// fillable, and if it does then populate our $data property
}
}
What I would like to do is that if I passed the following array to the Testing::fill() method, it'd update only the respective attributes that are considered fillable.
E.g., passing the following array
[
'active' => false,
'questions' => [
[
'question' => 'This is the first question',
'checked' => true,
],
[
'question' => 'This is the second question',
'checked' => true,
]
]
]
Would only modify the checked flags on the object, and everything else would be ignored - only marking the properties $data attribute questions.*.checked as true
I feel like there is a solution using Laravel's helpers but I just can't seem to come to it, or perhaps I am going the wrong way about it...
Ultimately, I just want some level of sanitisation so that when the entire structure is posted back to the objects fill method, only certain items can actually get updated (just like Laravel's fill method, just deeper with dynamic values). The problem is what is actually contained within $data is dynamic, so there might be one question, there might be 100...