akanaan/model-files
Composer 安装命令:
composer require akanaan/model-files
包简介
A package for laravel to help attaching files to models (images, pdfs, ... etc).
README 文档
README
A package for laravel to help attaching files to models (images, pdfs, ... etc).
Installation
Use the package manager composer to install Laravel Model Files.
composer require akanaan/model-files
Run migrate to create tables
php artisan migrate
Usage
The package is very easy to use, just add the trait HasFiles to the model and create settings array for the files
<?php namespace App\Models; use AKanaan\ModelFiles\Traits\HasFiles; use Illuminate\Database\Eloquent\Model; class Product extends Model { use HasFiles; protected $attaches = [ 'logo' => [ 'disk' => 'public', 'path' => '/', ], 'images' => [ 'disk' => 'public', 'path' => 'images', ] ]; }
Attach, Detach, Retrieve.
attaching
<?php ... $product = Product::findOrFail($id); $product->attachFile('logo', $request->file('logo')); $product->attachFiles('images', [ $request->file('images.0'), $request->file('images.1'), $request->file('images.2') ... ]); ...
detaching
<?php ... $product = Product::findOrFail($id); $product->detachFile('logo'); /* * Second param is optional (array of ids of files you want to delete) */ $product->detachFiles('images', [1, 2, 3]); ...
retrieving
<?php ... $product = Product::findOrFail($id); // returns instance of AKanaan\ModelFiles\Models\File $product->retrieveFile('logo'); // returns collection of AKanaan\ModelFiles\Models\File $product->retrieveFiles('images'); ...
Public Url
<?php ... $product = Product::findOrFail($id); $logo = $product->retrieveFile('logo'); $publicUrl = $logo->url(); ...
License
统计信息
- 总下载量: 10
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-10-04