laravelir / attachmentable
A package for attachment files to models
Fund package maintenance!
idpay.ir/laravelir
Installs: 28
Dependents: 0
Suggesters: 0
Security: 0
Stars: 4
Watchers: 2
Forks: 1
Open Issues: 0
pkg:composer/laravelir/attachmentable
Requires
- php: >=7.4|^8.0
- miladimos/laravel-toolkit: ^0.9.7
Requires (Dev)
- phpunit/phpunit: ^9.4
README
Laravel attachmentable package
A package for attachment files to models
Installation
- Run the command below to add this package:
composer require laravelir/attachmentable
- Open your config/attachmentable.php and add the following to the providers array:
Laravelir\Attachmentable\Providers\AttachmentableServiceProvider::class,
- Run the command below to install package:
php artisan attachmentable:install
- Run the command below to migrate database:
php artisan migrate
Uses
First add Attachmentable
trait to models that you want have attachments
namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Laravelir\Attachmentable\Traits\Attachmentable; class Post extends Model { use HasFactory, Attachmentable; }
and add Attachmentorable
trait to User models
namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Laravelir\Attachmentable\Traits\Attachmentorable; class User extends Model { use HasFactory, Attachmentorable; }
Methods
in controllers, with Attachmentable
trait you have these methods:
namespace App\Http\Controllers; use App\Models\Post; class PostController extends Controller { public function index() { $file = request()->file('thumbnail'); $post = Post::find(1); $post->attach($file, "posts/thumbnails"); $post->attachments // return all attachments $post->attachment($id); // return attachment $post->detach($id); $post->clearAttachments(); } }