codebuglab/laravel-media-removable 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

codebuglab/laravel-media-removable

Composer 安装命令:

composer require codebuglab/laravel-media-removable

包简介

Automatically remove media like images, videos, and audios from storage folder once record with media deleted or updated from database

README 文档

README

Automatically remove media like images, videos, and audios from storage folder once record with media deleted or updated from database

Laravel media removable

Table of contents

Setup

Installation

To install this package through composer run the following command in the terminal

composer require codebuglab/laravel-media-removable

Publish

You have to publish config file with this artisan command:

php artisan vendor:publish --provider="CodeBugLab\MediaRemovable\MediaRemovableServiceProvider"
  • File media-removable.php will be publish in config folder after that.
  • run php artisan config:clear to update application with published file.

Instructions

Here is multiple ways to use this package to delete media files from your project during updating and deleting database row using eloquent.

  • You have to use the trait CodeBugLab\MediaRemovable\MediaRemovable

Using The Model

  • To use model method all you have to do is to add a private static parameter mediaField
  • if you don't add mediaField automatically the package will throw and exception.
use CodeBugLab\MediaRemovable\MediaRemovable;

class MyModel extends Eloquent
{
    use MediaRemovable;

    private static $mediaFields = ['image']; //image is column_name you want to delete

}
  • by using the method the package will determine files path from config file which already set at config\media_removable.php
  • if you want to change file path for all the project you can change it from config file or use the private method mediaPath like this
use CodeBugLab\MediaRemovable\MediaRemovable;

class MyModel extends Eloquent
{
   use MediaRemovable;

   private static $mediaFields = ['image']; //image is column_name you want to delete

   private static $mediaPath = "storage/app/public/"; //The path of you media files
}
  • If you have a complex structure to your media files in your application you can use the private method mediaDetails instead
use CodeBugLab\MediaRemovable\MediaRemovable;

class MyModel extends Eloquent
{
   use MediaRemovable;

   // set all columns and paths for this model
   public static $mediaDetails = [
       [
           'field' => 'image',
           'path' => 'storage/app/public/images/'
       ],
       [
           'field' => 'profile_picture',
           'path' => 'storage/app/public/profile/'
       ],
   ];
}
  • Here you can determine every column folder path individually.

Using Config File

  • Alternatively you can use the config file to set all your media details instead of the model.
  • keep using MediaRemovable trait.
use CodeBugLab\MediaRemovable\MediaRemovable;

class MyModel extends Eloquent
{
    use MediaRemovable;
}
  • And then set change your config file as you want
return [
    'path' => 'storage/app/public/',

    'fields' => ['image']
];
  • The above code expect field image in all models used MediaRemovable trait, We don't think it's a practical way but we keep it if someone need it.
  • If you have more details than that you can use details instead.
return [
    'details' => [
        'table_name' => [
            [
                'field' => 'field_name',
                'path' => 'storage/app/public/directory/'
            ],
            [
                'field' => 'another_field_name',
                'path' => 'storage/app/public/another_directory/'
            ]
        ], 
        'another_table_name' => [
            [
                'field' => 'image',
                'path' => 'storage/app/public/'
            ]
        ]
    ]
];
  • Inside details array we use table name to track model details.
  • You can set multiple tables and fields as you can see, this way will make the model much cleaner specially if you have multiple fields.

Important Notice

  • The priority in this package is for config file information so if you set information to your config and your model, the model information will be overwrite by config information so be carful with that.

Testing

To run test use this command vendor/bin/phpunit

License

This package is a free software distributed under the terms of the MIT license.

统计信息

  • 总下载量: 27
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-10-11

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固