2017 © Pedro Peláez
 

library clamp-bolt

A Laravel package to attach/detach files to/from model.

image

sukohi/clamp-bolt

A Laravel package to attach/detach files to/from model.

  • Wednesday, June 13, 2018
  • by Sukohi
  • Repository
  • 1 Watchers
  • 0 Stars
  • 42 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 30 Versions
  • 5 % Grown

The README.md

ClampBolt

A Laravel package that allows you to attach/detach files to/from model.
(This package is maintained under L6.x), (*1)

Installation

Execute composer command., (*2)

composer require sukohi/clamp-bolt:5.*

Register ClampBoltServiceProvider in app.php if your Laravel version is less than 5.4., (*3)

'providers' => [
    // ...Others...,  
    Sukohi\ClampBolt\ClampBoltServiceProvider::class,
]

Preparation

First of all, execute publish and migrate command., (*4)

php artisan vendor:publish --provider="Sukohi\ClampBolt\ClampBoltServiceProvider"
php artisan migrate

And set ClampBoltTrait into your model., (*5)

use Sukohi\ClampBolt\ClampBoltTrait;

class Item extends Model
{
    use ClampBoltTrait;
}

That's all.
Now your model has 2 methods called attach() and detach()., (*6)

Usage

Attachment

[Basic way]:, (*7)

$item = \App\Item::find(1);
$item->attach('attachment_key', '/PATH/TO/YOUR/FILE');
$item->save();

[Multiple way]:, (*8)

$item = \App\Item::find(1);
$item->attach('attachment_key_1', '/PATH/TO/YOUR/FILE1');
$item->attach('attachment_key_2', '/PATH/TO/YOUR/FILE2');
$item->attach('attachment_key_3', '/PATH/TO/YOUR/FILE3');
$item->save();

// or

$item->attach([
    'attachment_key_1' => '/PATH/TO/YOUR/FILE1',
    'attachment_key_2' => '/PATH/TO/YOUR/FILE2',
    'attachment_key_3' => '/PATH/TO/YOUR/FILE3'
]);

[Parameters]:, (*9)

You can add parameters to each attachments., (*10)

$parameters = [
    'key_1' => 'value_1', 
    'key_2' => 'value_2', 
    'key_3' => 'value_3'
];
$item = \App\Item::find(1);
$item->attach('attachment_key', '/PATH/TO/YOUR/FILE', $parameters);
$item->save();

[Auto-Saving]:, (*11)

If you directly set Request parameter like the below, ClampBolt will automatically save the file in storage folder., (*12)

public function upload(Request $request, Item $item) {

    $item->attach('attachment_key', $request->file('photo'));
    $item->save();

}

Note: The file path is /storage/app/attachment_key/ in this case., (*13)

And if you use dot notation as follows, all of the files will be saved in photos.
I mean in the same folder., (*14)

public function upload(Request $request, Item $item) {

    $item->attach('photos.1', $request->file('photo_1'));
    $item->attach('photos.2', $request->file('photo_2'));
    $item->attach('photos.3', $request->file('photo_3'));
    $item->save();

    // or 

    $item->attach('photos.*', $path); // Refer to "Wildcard key"
    $item->save();

}

[Wildcard key]:, (*15)

You can use * to attach file(s)., (*16)

$item->attach('photos.*', $path);
$item->save();

In this case, this package will automatically save new attachment key like photos.0 or photos.1.
You also can multiply attach files., (*17)

$item->attach('photos.*', $path_1);
$item->attach('photos.*', $path_2);
$item->save();

And wildcard key is available for retrieving and detach files., (*18)

// Retrieve
$attachments = $item->getAttachment('photos.*');

// Detach
$item->detach('photos.*');
$user->save();

[Deleting old file]:, (*19)

If you'd like to delete old file, set true in the 4th argument., (*20)

$item->attach('attachment_key', '/PATH/TO/YOUR/NEW/FILE', [], true);

Detachment

[Basic way]:, (*21)

$item = \App\Item::find(1);
$item->detach('key');
$item->save();

[Multiple way]:, (*22)

$item = \App\Item::find(1);
$item->detach('key');
$item->detach('key2');
$item->detach('key3');
$item->save();

// or

$item->detach(['key', 'key2', 'key3']);
$item->save();

[All]:, (*23)

$item = \App\Item::find(1);
$item->detachAll();
$item->save();

[with Deleting file(s)], (*24)

$item->detach('key', true);
$item->save();

// or

$item->detach(['key', 'key2', 'key3'], true);
$item->save();

// or 

$item->detachAll(true);
$item->save();

Retrieve attachment

$item = \App\Item::find(1);
$attachment = $item->getAttachment($key);

echo $attachment->id;
echo $attachment->model;
echo $attachment->model_id;
echo $attachment->key;
echo $attachment->dir;
echo $attachment->filename;
echo $attachment->path;
echo $attachment->extension;
echo $attachment->mime_type;
echo $attachment->size;
echo $attachment->public_url;
echo $attachment->parameters;   // Array
echo $attachment->created_at;   // DateTime
echo $attachment->updated_at;   // DateTime

Note: public_url attribute is availabe if the file is stored in /storage/public. This means that you need to make a symbolic link in your Laravel app.
See here., (*25)

You also can get all attachments at once as follows., (*26)

$item = \App\Item::find(1);

foreach ($item->attachments as $key => $attachment) {

    // Do something..

}

[Filenames]:, (*27)

$filenames = $item->attachment_filenames;

[Paths]:, (*28)

$paths = $item->attachment_paths;

[Public URLs], (*29)

$public_urls = $item->attachment_public_urls;

Note:, (*30)

If you use dot-notation like array_key.0 for attachment key, attachment_filenames, attachment_paths and attachment_public_urls attributes return multi-dimensional array.
And values of attachment_public_urls is empty if they are not stored in /storage/public.
See Retrieve attachment in this file., (*31)

Check if attachment exists

if($item->hasAttachment('attachment_key')) {

    // has it!

}

// or multiply

if($item->hasAttachment('attachment_key.*')) {

    // has it!

}

Download

Call download() in your controller or routing., (*32)

return $attachment->download();

// or

return $attachment->download('filename.jpg');  

Response

Call response() in your controller or routing., (*33)

return $attachment->response();  

Events

You can call attached and detached events., (*34)

class Item extends Model
{
    protected $dispatchesEvents = [
        'attached' => ItemAttached::class,
        'detached' => ItemDetached::class
    ];

(in Your event), (*35)

class ItemAttached
{
    public function __construct(Attachment $attachment)
    {
        // Do someting..
    }

Note: The first argument of constructor is $attachment instance. Not parent model instance., (*36)

Where clause

whereHasAttachment() allows you to retrieve item(s) that have at least one attachment ., (*37)

$users = \App\User::whereHasAttachment('attachment_key')->get();

or you can use wildcard as follows., (*38)

$users = \App\User::whereHasAttachment('attachment_key.*')->get();

Validation rule

total_attachment:table,id,attachment_key,min,max,detaching_count

The total number of files under validation must be between :min and :max.
This total number is including files that already be stored in your app., (*39)

'photos' => 'total_attachment:users,1,photos,1,5'

Add detaching_count at the end of the parameters if you are detaching file at the same time of attaching., (*40)

'photos' => 'total_attachment:users,1,photos,1,5,3'

Then add an error message in /resources/lang/**/validation.php., (*41)

// en
'total_attachment' => 'The total number of :attribute must be between :min and :max.'

// ja
'total_attachment' => ':attributeの全件数は、:min個から:max個にしてください。'

Commands

Clear attachment

You can delete all attachment files and clear attachments table by running attachment:clear command., (*42)

php artisan attachment:clear

Or without confirmation, (*43)

php artisan attachment:clear --force

Note: Folder will not be deleted., (*44)

Relationship

ClampBoltTrait

You have two relationships called attachments and public_attachments.
The first one has all of data but public_attachments has only some data so that you can publish even for visitors like ajax data.
The keys are as follows., (*45)

  • id
  • model_id
  • key
  • filename
  • extension
  • mime_type
  • parameters
  • public_url

And usage with public_attachments., (*46)

$user = \App\User::with('public_attachments')->find(1);

Attachment

$user = \App\User::find(1);

foreach($user->attachments as $attachment) {

    $parent = $attachment->parent;  // \App\User

}

Thumbnail

You can generate thumbnail if attaching file is image as follows., (*47)

$user->attach('image', $file, [], true)->thumbnail([
    '500x300',
    '300x300'
]);

In this case, the attachment keys are image_thumbnail_500x300 and image_thumbnail_300x300., (*48)

Of course, wildcard is available., (*49)

$user->attach('photos.*', $file, [], true)->thumbnail([
    '700x500',
    '150x150'
]);

Or Attachment also has thumbnail()., (*50)

$width = 500;
$height = 300;
$attachment->thumbnail($width, $height);

When deleting

This package detaches all of attachment data corresponding to a record that you are deleting. If you also would like to delete attachment files, set true in the 1st argument., (*51)

$item->delete(true);

Set directory

If you'd like to save your uploaded file in a specific directory, use setAttachmentDir()., (*52)

$user->setAttachmentDir('public/nested_1/nested_2/nested_3');
$user->attach('attachment_key', $request->file('profile'));
$user->save();

In this case, the file will be stored in /storage/app/public/nested_1/nested_2/nested_3/attachment_key/., (*53)

License

This package is licensed under the MIT License., (*54)

Copyright 2016 Sukohi Kuhoh, (*55)

The Versions

13/06 2018

dev-master

9999999-dev

A Laravel package to attach/detach files to/from model.

  Sources   Download

MIT

The Requires

 

by Avatar Sukohi

13/06 2018

4.0.x-dev

4.0.9999999.9999999-dev

A Laravel package to attach/detach files to/from model.

  Sources   Download

MIT

The Requires

 

by Avatar Sukohi

13/06 2018

4.0.8

4.0.8.0

A Laravel package to attach/detach files to/from model.

  Sources   Download

MIT

The Requires

 

by Avatar Sukohi

25/04 2018

4.0.7

4.0.7.0

A Laravel package to attach/detach files to/from model.

  Sources   Download

MIT

The Requires

 

by Avatar Sukohi

25/04 2018

4.0.6

4.0.6.0

A Laravel package to attach/detach files to/from model.

  Sources   Download

MIT

The Requires

 

by Avatar Sukohi

16/04 2018

4.0.5

4.0.5.0

A Laravel package to attach/detach files to/from model.

  Sources   Download

MIT

The Requires

 

by Avatar Sukohi

16/04 2018

4.0.4

4.0.4.0

A Laravel package to attach/detach files to/from model.

  Sources   Download

MIT

The Requires

 

by Avatar Sukohi

17/09 2017

4.0.3

4.0.3.0

A Laravel package to attach/detach files to/from model.

  Sources   Download

MIT

The Requires

 

by Avatar Sukohi

25/06 2017

4.0.2

4.0.2.0

A Laravel package to attach/detach files to/from model.

  Sources   Download

MIT

The Requires

 

by Avatar Sukohi

21/06 2017

4.0.1

4.0.1.0

A Laravel package to attach/detach files to/from model.

  Sources   Download

MIT

The Requires

 

by Avatar Sukohi

21/06 2017

4.0.0

4.0.0.0

A Laravel package to attach/detach files to/from model.

  Sources   Download

MIT

The Requires

 

by Avatar Sukohi

07/10 2016

3.0.x-dev

3.0.9999999.9999999-dev

A Laravel package to attach/detach files to/from model.

  Sources   Download

MIT

The Requires

 

by Avatar Sukohi

07/10 2016

3.0.11

3.0.11.0

A Laravel package to attach/detach files to/from model.

  Sources   Download

MIT

The Requires

 

by Avatar Sukohi

07/10 2016

3.0.10

3.0.10.0

A Laravel package to attach/detach files to/from model.

  Sources   Download

MIT

The Requires

 

by Avatar Sukohi

07/10 2016

3.0.9

3.0.9.0

A Laravel package to attach/detach files to/from model.

  Sources   Download

MIT

The Requires

 

by Avatar Sukohi

07/10 2016

3.0.8

3.0.8.0

A Laravel package to attach/detach files to/from model.

  Sources   Download

MIT

The Requires

 

by Avatar Sukohi

04/10 2016

3.0.7

3.0.7.0

A Laravel package to attach/detach files to/from model.

  Sources   Download

MIT

The Requires

 

by Avatar Sukohi

13/09 2016

3.0.6

3.0.6.0

A Laravel package to attach/detach files to/from model.

  Sources   Download

MIT

The Requires

 

by Avatar Sukohi

13/09 2016

3.0.5

3.0.5.0

A Laravel package to attach/detach files to/from model.

  Sources   Download

MIT

The Requires

 

by Avatar Sukohi

26/08 2016

3.0.4

3.0.4.0

A Laravel package to attach/detach files to/from model.

  Sources   Download

MIT

The Requires

 

by Avatar Sukohi

13/07 2016

3.0.3

3.0.3.0

A Laravel package to attach/detach files to/from model.

  Sources   Download

MIT

The Requires

 

by Avatar Sukohi

13/07 2016

3.0.2

3.0.2.0

A Laravel package to attach/detach files to/from model.

  Sources   Download

MIT

The Requires

 

by Avatar Sukohi

13/07 2016

3.0.1

3.0.1.0

A Laravel package to attach/detach files to/from model.

  Sources   Download

MIT

The Requires

 

by Avatar Sukohi

13/07 2016

3.0.0

3.0.0.0

A Laravel package to attach/detach files to/from model.

  Sources   Download

MIT

The Requires

 

by Avatar Sukohi

13/06 2016

2.0.x-dev

2.0.9999999.9999999-dev

A Laravel package to attach/detach files to/from model.

  Sources   Download

MIT

The Requires

 

by Avatar Sukohi

27/05 2016

1.0.x-dev

1.0.9999999.9999999-dev

A Laravel package to attach/detach files to/from model.

  Sources   Download

MIT

The Requires

 

by Avatar Sukohi

27/05 2016

2.0.0

2.0.0.0

A Laravel package to attach/detach files to/from model.

  Sources   Download

MIT

The Requires

 

by Avatar Sukohi

27/05 2016

1.0.2

1.0.2.0

A Laravel package to attach/detach files to/from model.

  Sources   Download

MIT

The Requires

 

by Avatar Sukohi

27/05 2016

1.0.1

1.0.1.0

A Laravel package to attach/detach files to/from model.

  Sources   Download

MIT

The Requires

 

by Avatar Sukohi

27/05 2016

1.0.0

1.0.0.0

A Laravel package to attach/detach files to/from model.

  Sources   Download

MIT

The Requires

 

by Avatar Sukohi