williamoliveira / attachable

此包最新版本(v1.0-alpha.1)没有提供许可信息。

v1.0-alpha.1 2016-06-11 02:11 UTC

This package is auto-updated.

Last update: 2024-09-05 05:26:08 UTC


README

使用https://github.com/spatie/laravel-medialibrary

Attachable

将文件附加到Laravel Eloquent模型,与任何Laravel文件系统驱动器兼容

安装

composer require williamoliveira/attachable@dev-master
php artisan vendor:publish
php artisan migrate

使用方法

扩展Williamoliveira\Attachable\Models\AttachableModel并按需进行配置

<?php

namespace App\Models;

use App\Services\TenantContext;
use Intervention\Image\Image as InterventionImage;
use Williamoliveira\Attachable\Models\AttachableModel;

class Image extends AttachableModel
{
    // Set true if you want the model to accepts only images
    public $onlyImages = true;

    // Define the path where images will be stored, you can use id, filename, template and extension wildcards
    // (Fallsback to 'images_fallback_path' in your config file)
    public $imagesPath = 'images/{id}/{filename}--{template}.{extension}';
    
    // Define the path where files will be stored, you can use id, filename and extension wildcards
    // (Only for AttachableModel) 
    public $filesPath = 'files/{id}/{filename}.{extension}';
    
    // Define the default image template
    // (Optional, defaults to 'original') 
    protected $defaultTemplate = 'normal';
    
    // Define the Laravel Filesystem disk wich you be used to store the files
    // (Optional, fallsback to 'default_disk' on config file)
    public $disk = 'local_public';

    // Define your image modification, you can use anything from the Image Intervetion API
    // These image templates will be stored to disk, kinda like what Wordpress does, if you are familiar
    public function imageTemplates()
    {
        return [
            'original' => function (InterventionImage $image) {
                return $image;
            },
            'normal' => function (InterventionImage $image) {
                return $image->resize(800, null, function ($constraint) {
                    $constraint->aspectRatio();
                });
            },
            'thumbnail' => function (InterventionImage $image) {
                return $image->sharpen(10)->fit(200, 150);
            }
        ];
    }
}

由于它只是一个普通的Eloquent模型,您可以通过以下方式将其附加到其他Eloquent模型

//...
    public function image()
    {
        return $this->morphOne(\App\Models\Image::class, 'attachable');
    }
//...

AttachableModel具有以下属性

'file', // An URL string or an instance of Symfony\Component\HttpFoundation\File\UploadedFile
'file_name',
'file_extension',
'mime_type',
'file_size'

通常,您只需要设置文件属性,其他属性将根据文件自动设置

您可以使用任何Eloquent方式实例化AttachableModel,如下所示

$image = Image::create(['file' => $myUploadedFile]);

您可以使用$image->url($imageTemplateName)获取图像的公开URL,但目前仅适用于S3驱动器或名为'local_public'的磁盘上的本地公开(是的,我会稍后改进这一点)

提示

如果您想将图像存储在公共文件夹中,请像这样在config/filesystems.php中创建一个新的磁盘

'local_public' => [
  'driver' => 'local',
  'root'   => public_path('storage'),
],

然后更改config/attachable.php中'default_disk'属性为'local_public'