thibaud-dauce/migrations

迁移辅助工具,用于创建SQL视图、关系列等!

3.8.0 2023-02-28 07:16 UTC

This package is auto-updated.

Last update: 2024-08-28 10:58:23 UTC


README

此软件包解决了多个问题

  • 在迁移中定义关系既繁琐又容易出错,需要进行大量的复制粘贴
  • 在模型和迁移之间来回切换,以检查列的命名
  • 如何使用迁移和Eloquent QueryBuilder创建和刷新视图?

安装

composer require thibaud-dauce/migrations

用法

创建表

不要使用 Illuminate\Database\Migrations\Migration,而是使用 ThibaudDauce\Migrations\Migration,并在您的迁移中定义一个 protected $model 属性。

<?php

use ThibaudDauce\Migrations\Migration;
use App\Comment;

class CreateCommentsTable extends Migration
{
    protected $model = Comment::class;
}

接下来,您需要在模型的 schema 方法中定义您的表模式。如果您更喜欢在迁移中保持您的表模式,您可以在迁移中定义 schema

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Schema\Blueprint;

class Comment extends Model
{
    public function schema(Blueprint $table)
    {
        $table->increments('id');
        $table->string('author');
        $table->text('body');
        $table->timestamps();
    }
}

创建视图

对于您的应用程序创建SQL视图可能很有用。您知道您可以为您的视图创建Eloquent模型并将其用作任何其他Eloquent模型吗?查看 @Brendt 的非常好的博客文章:Eloquent MySQL views

创建视图的过程类似。不要使用 Illuminate\Database\Migrations\Migration,而是使用 ThibaudDauce\Migrations\ViewMigration,并在您的迁移中定义一个 protected $model 属性。

<?php

use ThibaudDauce\Migrations\ViewMigration;
use App\Search;

class CreateCommentsTable extends ViewMigration
{
    protected $model = Search::class;
}

接下来,您需要在模型的 schema 方法中定义您的视图查询。如果您更喜欢在迁移中保持您的视图查询,您可以在迁移中定义 schema。该 schema 方法必须返回一个 Illuminate\Database\Query\Builder

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class Search extends Model
{
    public function schema()
    {
        $titles = DB::table('posts')->select('title as term', 'id as searchable_id', Post::class . ' as searchable_type');
        $bodies = DB::table('posts')->select('body as term', 'id as searchable_id', Post::class . ' as searchable_type');
        $comments = DB::table('comments')->select('body as term', 'id as searchable_id', Comment::class . ' as searchable_type');

        return $titles->union($bodies)->union($comments);
    }
}

刷新视图

php artisan migrate:refresh-view database/migrations/2014_10_15_000000_create_searches_view.php

使用 relation 辅助工具

此软件包在 Blueprint 类中定义了一个宏,以快速创建关系列。

在您的模式定义中,使用 $table->relation('post') 来生成列和外键。对于以下 BelongsTo 关系

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Schema\Blueprint;

class Comment extends Model
{
    public function schema(Blueprint $table)
    {
        $table->increments('id');
        $table->string('author');
        $table->text('body');
        $table->relation('post');
        $table->timestamps();
    }
    
    public function post()
    {
        return $this->belongsTo(Post::class);
    }
}

这两个列被生成

$table->integer('post_id')->unsigned();
$table->foreign('post_id')->references('id')->on('posts');

在模式迁移之前和之后

您可以在模型内部定义 before_schema_migrationafter_schema_migration。这些方法在表创建之前和之后被调用。

例如,您可以使用 before_schema_migration 在创建表之前创建一些PostgreSQL枚举类型。

已知问题

  • PDO 在创建视图时禁止传递变量。您应使用方法的原始版本来绕过此限制(例如,使用 whereRaw 而不是 where)。
  • CREATE VIEW 与 PostgreSQL 和 SQLite 兼容。我还没有在其他数据库引擎上进行测试。如果需要,我们应该使用更好的方法来与其他数据库一起工作。

迁移到 2.0.0

  • 在您的迁移中,将 protected $class 重命名为 protected $model
  • 在您的视图迁移中,使用 ThibaudDauce\Migrations\ViewMigrations 扩展,而不是 ThibaudDauce\Migrations\Migration
  • 在您的视图模型中,将 view 方法重命名为 schema

待办事项

我愿意接受拉取请求 :-)

  • 添加多对多关系
  • 自动添加自增ID
  • 在创建视图时提供对其他SQL方言的更好支持