src/Traits/CrudModels.php
public static function syncHasMany($model, $campo, $children_items, $config) {
        $children = $model->{$campo};
        $children_items = collect($children_items);
        $deleted_ids = $children->filter(
                        function ($child) use ($children_items) {
                    return empty(
                            $children_items->where($config[$campo]['id'], $child->$config[$campo]['id'])->first()
                    );
                }
                )->map(function ($child) {
            $id = $child->id;
            $child->delete();
            return $id;
        }
        );
        $attachments = $children_items->filter(
                        function ($children_item) {
                    return empty($children_item->$config[$campo]['id']);
                }
                )->map(function ($children_item) use ($deleted_ids) {
            $children_item->$config[$campo]['id'] = $deleted_ids->pop();
            return new $config[$campo]['modelo']($children_item);
        });
        $model->{$campo}()->saveMany($attachments);
    }
public static function syncHasMany($model, $campo, $children_items, $config) {
        $children = $model->{$campo};
        $children_items = collect($children_items);
        $deleted_ids = $children->filter(
                        function ($child) use ($children_items) {
                    return empty(
                            $children_items->where($config[$campo]['id'], $child->$config[$campo]['id'])->first()
                    );
                }
                )->map(function ($child) {
            $id = $child->id;
            $child->delete();
            return $id;
        }
        );
        $attachments = $children_items->filter(
                        function ($children_item) {
                    return empty($children_item->$config[$campo]['id']);
                }
                )->map(function ($children_item) use ($deleted_ids) {
            $children_item->$config[$campo]['id'] = $deleted_ids->pop();
            return new $config[$campo]['modelo']($children_item);
        });
        $model->{$campo}()->saveMany($attachments);
    }
public static function syncHasMany ( $ model , $ campo , $ children_items , $ config ) { $ children = $ model -> { $ campo } ; $ children_items = collect ( $ children_items ) ; $ deleted_ids = $ children -> filter ( function ( $ child ) use ( $ children_items ) { return empty ( $ children_items -> where ( $ config [ $ campo ] [ 'id' ] , $ child -> $ config [ $ campo ] [ 'id' ] ) -> first ( ) ) ; } ) -> map ( function ( $ child ) { $ id = $ child -> id ; $ child -> delete ( ) ; return $ id ; } ) ; $ attachments = $ children_items -> filter ( function ( $ children_item ) { return empty ( $ children_item -> $ config [ $ campo ] [ 'id' ] ) ; } ) -> map ( function ( $ children_item ) use ( $ deleted_ids ) { $ children_item -> $ config [ $ campo ] [ 'id' ] = $ deleted_ids -> pop ( ) ; return new $ config [ $ campo ] [ 'modelo' ] ( $ children_item ) ; } ) ; $ model -> { $ campo } ( ) -> saveMany ( $ attachments ) ; }
Sync a HasMany relationsihp
@param object $model The model to sync
@param string $campo The name of the method with the hasMany relationsihp
@param array $children_items An array with the models to sync with
@param array $config The configuration array of the model
Sync a HasMany relationsihp