2

So I have the following code:

if ($obj->updated_date > $record->updated_date || $mode === 'refresh') {
    if (empty($obj->birthday) || $obj->hire_date) {
        $record->fill([
            'birthday' => '',
            'hire_date' => ''
        ]);
    } else {
        $record->fill($arr);
    }
} else {
    $record->timestamps = false;
}

Where I'm checking if $obj->birthday or $obj->hire_date is empty, and then define them as empty strings, but here is the problem.

I want to be able to call $record->fill($arr) regardless and prepopulate all my fields on the empty check, but for some reason I can't seem to figure out out.

So heres the logic:

Empty hire_date? set as ''.
Empty birthday? set as ''.
Populate the rest of the fields..

Both hire_date and birthday not empty? Populate all fields.

2 Answers 2

2

You can try this:

if ($obj->updated_date > $record->updated_date || $mode === 'refresh') {
    $filllArray = $arr;
    if (empty($obj->birthday) {
        $record->fill([
            'birthday' => ''
        ]);
        unset($filllArray['birthday']);
    }
    if (empty($obj->hire_date)) {
        $record->fill([
            'hire_date' => ''
        ]);
        unset($filllArray['hire_date']);
    }
    $record->fill($filllArray);
} else {
    $record->timestamps = false;
}

Update 1: Copy the $arr to $filllArray and update the new array to determine what should be filled or not.

Sign up to request clarification or add additional context in comments.

2 Comments

seems like the $record->fill($arr); ends up overwritting the results.
@Tripp Okay, I've updated my answer according to your comment. That should handle all your cases.
1

I know you've accepted an answer. This is a slightly different way of achieving the same thing (If I understood the question correctly)

if ($obj->updated_date > $record->updated_date || $mode === 'refresh') {
    $fa = [];
    empty($obj->birthday) ? $fa['birthday'] = '' : $fa['birthday'] = $obj->birthday;
    empty($obj->hire_date) ? $fa['hire_date'] = '' : $fa['hire_date'] = $obj->hire_date;
    $merged_arr = array_merge($arr, $fa);
    $record->fill($merged_arr);    
} else {
    $record->timestamps = false;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.