ahmed-aliraqi/eloquent-media

此软件包最新版本(v1.2.5)没有提供许可证信息。

将文件关联到您的 eloquent 模型

v1.2.5 2018-02-14 15:02 UTC

This package is auto-updated.

Last update: 2024-08-29 03:36:47 UTC


README

Build Status Total Downloads Latest Stable Version License

A php trait that's provides an easy way to associate images and/or files for your laravel eloquent models.

安装

composer require ahmed-aliraqi/eloquent-media

配置

在您的 config/filesystems.php 文件中,将 disks.local 键配置为与您首选的上传路径匹配。

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

使用方法

namespace App;

use Illuminate\Database\Eloquent\Model;
use Aliraqi\Traits\HasFiles;

class Post extends Model
{
    use HasFiles;


    //...
}

API

putFile

方法定义

/**
 * Upload given file to this model instance.
 *
 * @param  string  $key
 * @param  string  $name
 * @param  array   $options
 * @return string  File path
 */
$model->putFile($key, [$name, $options]);

注意:上传的文件将保存在 filesystems.php 文件 中配置的路径。

例如 $user->putFile('avatar'); 将保存上传的文件 avatar 并将其保存在 public/uploads/users/USER_ID/avatar.EXT,其中 USER_ID 是用户 ID,EXT 是上传文件的扩展名。

注意:如果您使用的是除了 $id 以外的主键,它将自动使用,而不是 $id。

file

方法定义

/**
 * Get link of given file name that belongs to this model instance.
 *
 * @param  string $name
 * @param  string $fallback
 * @return string
 */
$model->file([$name, $fallback]);

注意:获取上传文件的链接。

例如 $user->file('avatar'); 将获取上传文件 avatar 的链接 https://:8000/uploads/users/USER_ID/avatar.ext,其中 USER_ID 是用户 ID。

putFiles

方法定义

/**
 * Upload given files to this model instance.
 *
 * @param  string  $key
 * @param  string  $name
 * @param  boolean  $delete
 * @param  array  $options
 * @return string  File path
 */
$model->putFiles($key, [$name, $delete, $options]);

注意:多个上传的文件将保存在 filesystems.php 文件 中配置的路径。

例如 $user->putFiles('avatars'); 将保存上传的文件 avatars 并将其保存在 public/uploads/users/USER_ID/avatars/583ac3d5a0135.ext,其中 USER_ID 是用户 ID。

files

方法定义

/**
* Get array of given files name that belongs to this model instance.
*
* @param  string $name  name of folder
* @return Illuminate\Support\Collection
*/
public function files($name)

注意:获取上传文件的链接。

例如 $user->files('avatars'); 将获取上传文件 avatars 的路径和链接的数组集合。例如

@foreach($user->files('avatar') as $path => $link)
 file path : {{ $path }} <br>
 File link : {{ $link }}
@endforeach

filePath

方法定义

/**
 * Get path of given file name that belongs to this model instance.
 *
 * @param  string $name
 * @return string | null
 */
public function filePath([$name])

hasGlobal

方法定义

/**
 * Determine if the assiciated files is global or not.
 *
 * @param  boolean $value
 * @return Illuminate\Database\Eloquent\Model
 */
public function hasGlobal([$value = true])

例如

$user->hasGlobal()->putFile('default');

文件将保存在 public/uploads/users/default.png

{{ $user-hasGlobal()->file('default') }}

disk

方法定义

/**
 * Determine a filesystem instance.
 *
 * @param  string  $name
 * @return Illuminate\Database\Eloquent\Model
 */
public function disk([$name = 'local'])

例如

$user->disk('public')->putFile('photo');
{{ $user->disk('public')->file('photo') }}

备用图片。

如果您希望在没有找到指定图片时返回备用图片,您必须创建 config/fallbackimages.php 文件,并设置模型表名和备用图片,如下所示

<?php
return [
    // Get users fallback image url.
    'users' => 'http://lorempixel.com/grey/800/400/cats/Faker/',

    // Get posts fallback image url.
    'posts' => 'http://lorempixel.com/grey/800/400/cats/Faker/',

	// If You do not run application using artisan serve.
    'remove_public_from_url' => false,
];