I am parsing through the Laravel request object to identify if any inputs were entered by the user, so that I may update the model with only the updated values.
Currently I am using:
if (isset($request->$field) && $request->$field != $model->$field) {
$model->$field = $request->$field;
}
and I recently found a Laravel request method whenHas that seems to be a more efficient way to accomplish this task.
I searched the web and stackoverflow for examples but only found the basic construct. I am new to OOP but do understand this would be related to closures/callbacks.
FYI, in my current code I have the isset() function since the response may contain a null value for the field that I am comparing against the model's related field value (my current code snippet is within a "foreach($fields as $field) function).
How would I accomplish this functionality with the whenHas method?
Any help would be appreciated, thank you!
question from:
https://stackoverflow.com/questions/65867319/how-to-use-laravel-request-whenhas-method 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…