alex-oliveira/ao-comments

Laravel 评论资源的工具。

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

This package is auto-updated.

Last update: 2024-09-25 23:00:09 UTC


README

1) 安装

$ composer require alex-oliveira/ao-comments

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

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

3) 创建 "config/ao.php" 文件

return [
    .
    .
    .
    'models' => [
        'users' => App\Models\User::class,
    ],
        
    'tables' => [
        'users' => 'users'
    ]
    .
    .
    .
];

4) 发布迁移

$ php artisan vendor:publish
$ composer dump

使用方法

迁移

向上

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

与向下相同

public function up()
{    
    Schema::create('ao_comments_x_posts', function (Blueprint $table) {
        $table->integer('post_id')->unsigned();
        $table->foreign('post_id', 'fk_posts_x_ao_comments')->references('id')->on('posts');
        
        $table->bigInteger('comment_id')->unsigned();
        $table->foreign('comment_id', 'fk_ao_comments_x_posts')->references('id')->on('ao_comments_comments');
        
        $table->primary(['post_id', 'comment_id'], 'pk_ao_comments_x_posts');
    });
}

向下

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

与向下相同

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

模型

namespace App\Models;

use AoComments\Models\Comment;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{

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

与向下相同

return $this->belongsToMany(Comment::class, 'ao_comments_x_posts');

控制器

namespace App\Http\Controllers\Posts;

use AoComments\Controllers\AoCommentsController;
use App\Models\Post;

class CommentsController extends AoCommentsController
{

    protected $dynamicClass = Post::class;
    
}

路由

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

    AoComments()->router()->controller('Posts\CommentsController')->foreign('post_id')->make();
    .
    .
    .
    
});

检查路由

$ php artisan route:list