area17 / twill

Twill is an open source CMS toolkit for Laravel that helps developers rapidly create a custom admin console that is intuitive, powerful and flexible. Chat with us on Discord at https://discord.gg/cnWk7EFv8R.
https://twillcms.com
Apache License 2.0
3.75k stars 572 forks source link

Improve related save #2599

Open Tofandel opened 4 months ago

Tofandel commented 4 months ago

A quick PR to improve saveRelated to work with models, I wanted to use it and according to the phpdoc it accepts an array of Twill model, but this is not the case (it only accepts an array with id and endpointType)

This PR allows the method to accept models as well and improves the performance by using only one where instead of 3 which would iterate 3 times over the collection

I also added $item->unsetRelation('related') in loadRelated, otherwise there is a circular reference in the model relation (model -> pivot -> related -> model ...) which causes an infinite loop when using model->toArray()