Download the PHP package phoenixfire296/laravel-attachments without Composer
On this page you can find all versions of the php package phoenixfire296/laravel-attachments. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download phoenixfire296/laravel-attachments
More information about phoenixfire296/laravel-attachments
Files in phoenixfire296/laravel-attachments
Package laravel-attachments
Short Description Laravel package to link file attachment to models leveraging Eloquent SoftDelete
License MIT
Informations about the package laravel-attachments
Laravel 5.x file attachment helpers
This package allows to quickly links files to models.
Can also be installed on Laravel 5.4, see below
Installation
You can install this package via composer. Laravel 5.5+ auto discovers the service provider.
composer require bnbwebexpertise/laravel-attachments
Older Laravel 5.4 install
For 5.4 support install version 0.0.16 :
composer require bnbwebexpertise/laravel-attachments:0.0.16
Then add the service provider to your configuration :
Configuration
You can customize this package behavior by publishing the configuration file :
php artisan vendor:publish --provider='Bnb\Laravel\Attachments\AttachmentsServiceProvider'
Add attachments to a model class
Add the HasAttachment
trait to your model class :
Then use it to bind a file to an instance :
Retrieve model's attachments
Regroup model's attachments
The group
attribute allows to group attachements.
The attachementsGroup
method provided by the HasAttachment
trait
returns all the attachments with the given group
value.
Delete an attachment
Calling the delete()
method on an attachment model instance will
delete the database row and the file. The deletion of the file can
be disabled by setting the behaviors.cascade_delete
to false
in
the configuration.
Not that calling
delete()
on aquery()
like statement will not cascade to the filesystem because it will not call thedelete()
method of theAttachment
model class.
Hooking the file output
The Bnb\Laravel\Attachments\Attachment
model class provides
an outputting
event that you can observe.
In the application service provider you could write for example :
Dropzone
Upload
This package provides a server endpoint for Dropzone.js or equivalent
via the attachments.dropzone
route alias.
It returns the attachment uuid
along other fields as a JSON response.
This value can be sent back later to the server to bind it to a model
instance (deferred saving).
The form :
The response :
Send it back later :
Bind the value later :
Delete
The route attachments.dropzone.delete
can be called via HTTP DELETE
.
The attachment ID must be provided as parameter.
The delete action provided by this route only works for pending attachement (not bound to a model).
To prevent deletion of other users file, the current CSRF token is saved when uploading via the dropzone endpoint and it must be the same when calling the dropzone delete endpoint. This behavior can be deactivated via the configuration or env key (see config/attachments.php).
Usage example :
Events
Two event are fired by the dropzone endpoints controller :
attachments.dropzone.uploading
with the$request : Request
as parameterattachments.dropzone.deleting
with the$request : Request
and the$file : Attachement
as parameters
If one of the listeners returns false, the action is aborted.
Temporary URLs
It is possible to generate a unique temporary URL for downloading the attachments via the getTemporaryUrl
method of the Attachment
model, for sharing purposes foremost.
The getTemporaryUrl
method has one parameter : a Carbon
date, after which the link will no longer be valid.
The default generated URL is of the form : http://example.com/attachments/shared/<a very long string>
. The share path can be modified in the config file under the shared_pattern
key.
Cleanup commands
A command is provided to cleanup the attachments not bound to a model
(when model_type
and model_id
are null
).
php artisan attachment:cleanup
The -s
(or --since=[timeInMinutes]
) option can be set to specify
another time limit in minutes : only unbound files older than the
specified age will be deleted. This value is set to 1440 by default.
Customization
Set a custom database connection name for the models
You can customize the database connection name by either :
- Adding an
.env
variable forATTACHMENTS_DATABASE_CONNECTION
(recommended) OR - Changing the configuration option
attachments.database.connection
inconfig/attachments.php
.
Extends Attachment model columns
The configuration defines the list of fillable attachment attributes in the attachment.attributes
key.
This allows you to create migration to add new columns in the attachment table
and declare them in your published config at config/attachments.php
.
Customize the attachment storage directory prefix
You may easily customize the folder/prefix where new attachments are stored by either:
- Adding an
.env
variable forATTACHMENTS_STORAGE_DIRECTORY_PREFIX
(recommended) OR - Changing the configuration option
attachments.storage_directory.prefix
inconfig/attachments.php
.
The default value is attachments
and any trailing /
s will be trimmed automatically.
Customize the attachment storage filepath
If you don't want to use the default storage filepath generation, you can provide the filepath
option (relative to the root of storage disk).
It must contain the directory and filename. It's up to you to ensure that the provided filepath is not in conflict with another file.
This does not apply to attachments uploaded via the integrated DropZone controller. Only available for explicit attachments.
Extending the Attachment model class
This can be helpful to add some relations to the attachment model.
Create your own model that extends Bnb\Laravel\Attachments\Attachment
:
Bind your model to the Bnb\Laravel\Attachments\Contracts\AttachmentContract
interface in a service provider :
All versions of laravel-attachments with dependencies
illuminate/database Version 5.7.x|5.6.x|5.5.x
illuminate/routing Version 5.7.x|5.6.x|5.5.x
illuminate/support Version 5.7.x|5.6.x|5.5.x
illuminate/console Version 5.7.x|5.6.x|5.5.x
illuminate/encryption Version 5.7.x|5.6.x|5.5.x
bnbwebexpertise/php-uuid Version >=0.0.2
doctrine/dbal Version ~2.5
nesbot/carbon Version ~1.20