In PHP / Laravel, I am looking for an easier way to do the following in one line:
$newItem = clone $item;
unset($newItem->property1);
unset($newItem->property2);
I know I can create a helper method for it, but maybe there is one in Laravel already and i wasn’t aware of.
2
Answers
you can also define a
__clone
method in your objectand if your
$item
is a model you can do:There sure is a one liner and is mostly only useful for scenarios where you want to unset multiple properties.
Convert the object to an array via typecasting, filter the keys that are required and cast them back to an object.
Live Demo