PHPackages                             cyneek/laravel-multiple-stapler - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [File &amp; Storage](/categories/file-storage)
4. /
5. cyneek/laravel-multiple-stapler

ActiveLibrary[File &amp; Storage](/categories/file-storage)

cyneek/laravel-multiple-stapler
===============================

Multiupload for Laravel Stapler.

v0.1.1(9y ago)25033[1 PRs](https://github.com/Patroklo/laravel-multiple-stapler/pulls)MITPHPPHP &gt;=5.4.0

Since Apr 27Pushed 8y ago1 watchersCompare

[ Source](https://github.com/Patroklo/laravel-multiple-stapler)[ Packagist](https://packagist.org/packages/cyneek/laravel-multiple-stapler)[ RSS](/packages/cyneek-laravel-multiple-stapler/feed)WikiDiscussions master Synced yesterday

READMEChangelog (2)Dependencies (2)Versions (3)Used By (0)

Laravel Stapler Multiple File Handler
=====================================

[](#laravel-stapler-multiple-file-handler)

[Spanish Readme](https://github.com/Patroklo/laravel-multiple-stapler/blob/master/README_ES.md)
-----------------------------------------------------------------------------------------------

[](#spanish-readme)

Index
-----

[](#index)

- [Changes](#changes)
- [Install](#install)
- [Description](#description)
    - [Methods](#methods)
- [Configuration of Stapler properties](#configuration-of-stapler-properties)
    - [Declaring single file properties](#declaring-single-file-properties)
    - [Declaring multiple file properties](#declaring-multiple-file-properties)
- [File insertion](#file-insertion)
    - [Inserting files in single file properties](#inserting-files-in-single-file-properties)
    - [Inserting files in multiple file properties](#inserting-files-in-multiple-file-properties)
- [Acessing uploaded file data](#acessing-uploaded-file-data)
    - [Acessing single file parameters](#acessing-single-file-parameters)
    - [Acessing multiple file parameters](#acessing-multiple-file-parameters)
- [Deleting linked files](#deleting-linked-files)
    - [Explicit deletion of single file properties](#explicit-deletion-of-single-file-properties)
    - [Explicit deletion of multiple file properties](#explicit-deletion-of-multiple-file-properties)
- [Known problems](#known-problems)

Changes
-------

[](#changes)

#### v0.1.1

[](#v011)

Changed the method `hasAttachedFile` into `hasOneAttachedFile` to improve compatibility with the `CodeSleeve/laravel-stapler` package. Now you can use both packages at the same time in your models.

Install
-------

[](#install)

Add this package with composer with the following command:

```
composer require cyneek/laravel-multiple-stapler
```

After updating composer, add the service providers to the `providers` array in `config/app.php`.

```
Codesleeve\LaravelStapler\Providers\L5ServiceProvider::class,
Cyneek\LaravelMultipleStapler\Providers\LaravelMultipleStaplerProvider::class
```

From the command line, use the migration tool; it will make a basic table in charge of handling all the data from the files linked to the application Models.

```
php artisan migrate
```

Aaaaand it's done!

Description
-----------

[](#description)

### Methods

[](#methods)

- **hasOneAttachedFile**: Method to link a parameter with only one file.
- **hasMultipleAttachedFiles:** Method that allows linking multiple files gallery-like into one parameter.

The parameters that both methods accept are:

- **name**: \[String|Required\] The name that will be assigned to the file parameter in the Model.
- **options**: \[Array|Required\] Stapler options that wil be used to handle the linked file. If you want to know more about the available options in this library, [you can click here to read the Stapler oficial documentation.](https://github.com/CodeSleeve/stapler)
- **attachedModelClass**: \[String|Optional\] Here you can define a model classname that will be used to handle the file information instead of the default one: `StaplerFiles`. The Model must implement the `LaravelStaplerInterface` interface.

Configuration of Stapler properties
-----------------------------------

[](#configuration-of-stapler-properties)

### Declaring single file properties

[](#declaring-single-file-properties)

This is the usual behavior in the Stapler library for Laravel. It lets uploading one single file and linking it into a loaded Model property, allowing all the usual operations that could be made in a vanilla Stapler file attachment.

To make a new property that links to a single file in a Model, you have to follow the next steps:

1. Add the `MultipleFileTrait` trait into the model

```
class Example extends \Eloquent
{
    use MultipleFileTrait;
```

2. Add into the `__construct()` method the properties you want to add using `hasOneAttachedFile` method.

```
    function __construct(array $attributes = [] )
    {

        $this->hasOneAttachedFile('avatar', [
            'styles' => [
                'medium' => '300x300',
                'thumb' => '100x100'
            ]
        ]);

        parent::__construct($attributes);

    }
```

Warning: It's required to add the parameter creation methods BEFORE the construct parent calling.

### Declaring multiple file properties

[](#declaring-multiple-file-properties)

This behavior it's possible thanks to the polymorphyc tables from Laravel, they will store all the file data linking it with their parent Model objects thanks to the fields `fileable_id`, `fileable_type` and `fileable_field` that will store the parameter name.

To make a multiple file handler property in a model, you have to follow the next steps:

1. Add the `MultipleFileTrait` trait into the model

```
class Example extends \Eloquent
{
    use MultipleFileTrait;
```

2. Add into the `__construct()` method the properties you want to add using `hasMultipleAttachedFiles` method.

```
    function __construct(array $attributes = [] )
    {

        $this->hasMultipleAttachedFiles('images', [
            'styles' => [
                'medium' => '300x300',
                'thumb' => '100x100'
            ]
        ]);

        parent::__construct($attributes);

    }
```

Warning: It's required to add the parameter creation methods BEFORE the construct parent calling.

File insertion
--------------

[](#file-insertion)

### Inserting files in single file properties

[](#inserting-files-in-single-file-properties)

For this example we will use a form that will upload a single file into our Model property. It's possible to use form fields that accept multiple files, but in those cases, all except the first uploaded file will be automatically discarded.

You must keep in mind that when you are making an `update` operation with a form, if there is a previous file linked in that property, it will be automatically deleted if you upload a newer one.

#### Form view

[](#form-view)

```
