alex-oliveira/ao-files

Laravel 文件资源

dev-master 2017-05-30 06:30 UTC

This package is auto-updated.

Last update: 2024-09-25 23:44:19 UTC


README

1) 安装

$ composer require alex-oliveira/ao-files

2) 配置 "config/app.php" 文件

'providers' => [
    /*
     * Vendor Service Providers...
     */
    AoFiles\ServiceProvider::class,
],

3) 发布迁移

$ php artisan vendor:publish
$ composer dump

使用方法

迁移

public function up()
{
    AoFiles()->schema()->create('posts');
}

public function up()
{    
    Schema::create('ao_files_x_posts', function (Blueprint $table) {
        $table->integer('post_id')->unsigned();
        $table->foreign('post_id', 'fk_posts_x_ao_files')->references('id')->on('posts');
        
        $table->bigInteger('file_id')->unsigned();
        $table->foreign('file_id', 'fk_ao_files_x_posts')->references('id')->on('ao_files_files');
        
        $table->primary(['post_id', 'file_id'], 'pk_ao_files_x_posts');
    });
}

public function down()
{
    AoFiles()->schema()->drop('posts');
}

public function down()
{    
    Schema::dropIfExists('ao_files_x_posts');
}

模型

namespace App\Models;

use AoFiles\Models\File;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{

    /**
     * @return File[]|\Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function files()
    {
        return $this->belongsToMany(File::class, AoFiles()->schema()->table($this->getTable()));
    }
    
}

return $this->belongsToMany(File::class, 'ao_files_x_posts');

控制器

namespace App\Http\Controllers\Posts;

use AoFiles\Controllers\AoFilesController;
use App\Models\Post;

class FilesController extends AoFilesController
{

    protected $dynamicClass = Post::class;
    
    protected $subFolders = ['posts' => 'post_id'];
    
}

路由

Route::group(['prefix' => 'posts', 'as' => 'posts.'], function () {

    AoFiles()->router()->controller('Posts\FilesController')->foreign('post_id')->make();
    .
    .
    .
    
});

检查路由

$ php artisan route:list