JPS Attachment is a laravel package that enlighten the way of attaching media on any eloquent model.
With JPS Attachment, you can share attachment with anyone, grant access a certain file to a certain owner.
php artisan vendor:publish --provider="Jalameta\\Attachments\\AttachmentServiceProvider"
We provide traits AttachmentCreator
to create an attachment.
Utilize the create()
method. First parameter is an UploadedFile instance and the second parameter accept array of attributes.
use Jalameta\Attachments\Concerns\AttachmentCreator;
class YourController {
use AttachmentCreator;
public function store()
{
$attachment = $this->create(request()->file('upload'), [
'title' => 'My Amazing Attachment.png',
]);
//
}
}
Utilize the createFromPath()
method. First parameter accept disk name defined
in your config/filesystems.php
(See more at Laravel Filesystem) and the second parameter accept array of attributes.
use Jalameta\Attachments\Concerns\AttachmentCreator;
class YourController {
use AttachmentCreator;
public function store()
{
$attachment = $this->createFromPath(request()->file('upload'), [
'disk' => 'attachment',
'title' => 'My Amazing Attachment.png',
]);
//
}
}
JPS Attachment provide AttachableContract
Interface and Attachable
trait to use
Example
use Jalameta\Attachments\Contracts\AttachableContract;
use Illuminate\Database\Eloquent\Model;
class YourModel extends Model implements AttachableContract
{
use Attachable;
}
$attachment = $this->create(request()->file('upload'), [
'title' => 'My Amazing Attachment.png',
]);
$model->attachments()->attach($attachment);
You may have any struggle to creating response based on saved attachments.
We provide AttachmentResponse
that bind into Application Container.
All you need is to resolve our AttachmentResponse instance by
use Jalameta\Attachments\AttachmentResponse;
$response = app(AttachmentResponse::class);
Example creating response.
$response->stream($attachment);
$response->download($attachment);
$response->streamOrDownload($attachment);
If you are using streamOrDownload()
method and pass query parameter stream
with value that equal with true
,
it will create stream response instead of download. If the stream
parameter is not present, the download action will be used.
JPS Attachment is flexible, it can be adjusted as your application needs. There are attributes that can be customized for.
Attribute | Required | Description |
---|---|---|
title | yes | File attachment title, it will be used in your download filename. |
mime | yes | Attachment file mime. |
disk | no | The filesystem disk target to save the attachment. if it's null the default filesystem disk that defined in config/attachment.php will be used. |
path | yes | After saving, generated filename will be stored in specific path. |
type | yes | default value attachment . |
description | no | Just text description. |
options | no | If you need complex logic with unique json value, you can store it here. |
- create unit tests