Php Laravel:数据库-更多“;日志列“;
我想在处创建的Php Laravel:数据库-更多“;日志列“;,php,laravel,laravel-5,Php,Laravel,Laravel 5,我想在处创建的列和处更新的列旁边添加一些其他列,例如: deleted\u在 creator 更新程序 deleter 最后三个应该包含user.id 更好的方法是什么 A.将其放入迁移 B.编辑蓝图类 不要直接编辑Laravel的核心蓝图类,您应该根据您的具体需求对其进行扩展并添加功能。下面是一个如何做到这一点的示例 在database目录中创建一个CustomBlueprint类,该类扩展了核心Blueprint类,其中包含自定义列的定义 <?php namespace Dat
列和
处更新的列旁边添加一些其他列,例如:
deleted\u在
creator
更新程序
deleter
最后三个应该包含user.id
更好的方法是什么
- A.将其放入
迁移
- B.编辑
蓝图
类
不要直接编辑Laravel的核心蓝图
类,您应该根据您的具体需求对其进行扩展并添加功能。下面是一个如何做到这一点的示例
在database
目录中创建一个CustomBlueprint
类,该类扩展了核心Blueprint
类,其中包含自定义列的定义
<?php
namespace Database;
class CustomBlueprint extends \Illuminate\Database\Schema\Blueprint
{
public function customColumns() {
$this->integer('creator')->nullable();
$this->integer('updater')->nullable();
$this->integer('deleter')->nullable();
}
}
然后创建迁移,例如
php artisan make:migration create_tests_table
在迁移文件中,使用如下方法
<?php
use Database\CustomBlueprint;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Migrations\Migration;
class CreateTestsTable extends Migration
{
public function up()
{
$schema = DB::connection()->getSchemaBuilder();
$schema->blueprintResolver(function($table, $callback) {
return new CustomBlueprint($table, $callback);
});
$schema->create('tests', function($table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
$table->customColumns();
});
}
public function down()
{
Schema::dropIfExists('tests');
}
}
所有迁移都需要它吗?@SazzadurRahman是的,我在所有项目中都尝试过,但我得到了[Symfony\Component\Debug\Exception\FatalThrowableError]类“Database\CustomBlueprint”在迁移运行时未找到
composer dumpautoload
,应该会得到修复,我也在用这个补丁更新答案
<?php
use Database\CustomBlueprint;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Migrations\Migration;
class CreateTestsTable extends Migration
{
public function up()
{
$schema = DB::connection()->getSchemaBuilder();
$schema->blueprintResolver(function($table, $callback) {
return new CustomBlueprint($table, $callback);
});
$schema->create('tests', function($table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
$table->customColumns();
});
}
public function down()
{
Schema::dropIfExists('tests');
}
}