src/Repositories/EloquentRepository.php
public function attachFiles($model, Request $request)
    {
        // Get the collection of files to add.
        $fileIds = $request->only('files')['files'];
        $files = File::whereIn('id', $fileIds)->get();

        // Empty collection that will contain files that are in selected directories.
        $subFiles = collect();

        // Remove folders and build array of ids.
        $newFiles = $files->each(function (Model $item) use ($subFiles) {
            if ($item->type === 'f') {
                foreach ($item->children as $file) {
                    if ($file->type !== 'f') {
                        // Add files in this directory to collection of SubFiles
                        $subFiles->push($file);
                    }
                }
            }
        })->reject(function (Model $item) {
            return $item->type === 'f';
        })
            ->pluck('id')
            ->toArray();

        // Transform subfiles collection to array of ids.
        $subFiles = $subFiles->pluck('id')->toArray();

        // Merge files with files in directory.
        $newFiles = array_merge($newFiles, $subFiles);

        // Get files that are already in the gallery.
        $filesIds = $model->files->pluck('id')->toArray();

        // Merge with new files.
        $files = array_unique(array_merge($filesIds, $newFiles));
        $number = count($files) - count($filesIds);

        // Prepare synchronisation.
        $pivotData = [];
        $position = 1;
        foreach ($files as $fileId) {
            $pivotData[$fileId] = ['position' => $position++];
        }

        // Sync.
        $model->files()->sync($pivotData);

        $this->forgetCache();

        return response()->json([
            'number' => $number,
            'message' => __(':number items were added.', compact('number')),
            'models' => $model->files()->get(),
        ]);
    }
public function attachFiles($model, Request $request)
    {
        // Get the collection of files to add.
        $fileIds = $request->only('files')['files'];
        $files = File::whereIn('id', $fileIds)->get();

        // Empty collection that will contain files that are in selected directories.
        $subFiles = collect();

        // Remove folders and build array of ids.
        $newFiles = $files->each(function (Model $item) use ($subFiles) {
            if ($item->type === 'f') {
                foreach ($item->children as $file) {
                    if ($file->type !== 'f') {
                        // Add files in this directory to collection of SubFiles
                        $subFiles->push($file);
                    }
                }
            }
        })->reject(function (Model $item) {
            return $item->type === 'f';
        })
            ->pluck('id')
            ->toArray();

        // Transform subfiles collection to array of ids.
        $subFiles = $subFiles->pluck('id')->toArray();

        // Merge files with files in directory.
        $newFiles = array_merge($newFiles, $subFiles);

        // Get files that are already in the gallery.
        $filesIds = $model->files->pluck('id')->toArray();

        // Merge with new files.
        $files = array_unique(array_merge($filesIds, $newFiles));
        $number = count($files) - count($filesIds);

        // Prepare synchronisation.
        $pivotData = [];
        $position = 1;
        foreach ($files as $fileId) {
            $pivotData[$fileId] = ['position' => $position++];
        }

        // Sync.
        $model->files()->sync($pivotData);

        $this->forgetCache();

        return response()->json([
            'number' => $number,
            'message' => __(':number items were added.', compact('number')),
            'models' => $model->files()->get(),
        ]);
    }
public function attachFiles ( $ model , Request $ request ) { // Get the collection of files to add. $ fileIds = $ request -> only ( 'files' ) [ 'files' ] ; $ files = File :: whereIn ( 'id' , $ fileIds ) -> get ( ) ; // Empty collection that will contain files that are in selected directories. $ subFiles = collect ( ) ; // Remove folders and build array of ids. $ newFiles = $ files -> each ( function ( Model $ item ) use ( $ subFiles ) { if ( $ item -> type === 'f' ) { foreach ( $ item -> children as $ file ) { if ( $ file -> type !== 'f' ) { // Add files in this directory to collection of SubFiles $ subFiles -> push ( $ file ) ; } } } } ) -> reject ( function ( Model $ item ) { return $ item -> type === 'f' ; } ) -> pluck ( 'id' ) -> toArray ( ) ; // Transform subfiles collection to array of ids. $ subFiles = $ subFiles -> pluck ( 'id' ) -> toArray ( ) ; // Merge files with files in directory. $ newFiles = array_merge ( $ newFiles , $ subFiles ) ; // Get files that are already in the gallery. $ filesIds = $ model -> files -> pluck ( 'id' ) -> toArray ( ) ; // Merge with new files. $ files = array_unique ( array_merge ( $ filesIds , $ newFiles ) ) ; $ number = count ( $ files ) - count ( $ filesIds ) ; // Prepare synchronisation. $ pivotData = [ ] ; $ position = 1 ; foreach ( $ files as $ fileId ) { $ pivotData [ $ fileId ] = [ 'position' => $ position ++ ] ; } // Sync. $ model -> files ( ) -> sync ( $ pivotData ) ; $ this -> forgetCache ( ) ; return response ( ) -> json ( [ 'number' => $ number , 'message' => __ ( ':number items were added.' , compact ( 'number' ) ) , 'models' => $ model -> files ( ) -> get ( ) , ] ) ; }
Attach files to a model.

@param \Illuminate\Database\Eloquent\Model $model
@param \Illuminate\Http\Request            $request

@return null
Attach files to a model .