gpt4 book ai didi

php - 扩展 Laravel 5.5/5.6 蓝图

转载 作者:行者123 更新时间:2023-12-01 10:23:16 25 4
gpt4 key购买 nike

如何扩展 Laravel 5.5/5.6 MySQL 蓝图 使用自定义方法 $table->myMethod() 像预建方法 $table->timestamps(); ?

我用 SQLlite 试过一个例子:Extending Blueprint for SQLite in Laravel 5

但我无法让它工作。我正在开发我自己的包(使用包开发最佳实践)并希望从那里包含 Blueprint 扩展。

我开发的包有 Laravel 扩展文件夹“扩展”:
./packages/vendorname/packagename/src/Extension/

有文件:

1) ./packages/vendorname/packagename/src/Extension/Blueprint.php

<?php

namespace Vendorname\Packagename\Extension;

use Illuminate\Database\Schema\Blueprint as ParentBlueprint;
use Illuminate\Support\Facades\DB;

class Blueprint extends ParentBlueprint
{

public function myMethod()
{
$this->text('custom_column')->default('Some value');
$this->text('custom_column2')->default('Some value2');
}
}

2) ./packages/vendorname/packagename/src/Extension/MySqlConnection.php

<?php

namespace Vendorname\Packagename\Extension;

use Vendorname\Packagename\Extension\Blueprint;
use Illuminate\Database\MySqlConnection as ParentMySqlConnection;
use Illuminate\Database\Schema\MySqlBuilder;

class MySqlConnection extends ParentMySqlConnection
{

public function getSchemaBuilder()
{
if (is_null($this->schemaGrammar)) {
$this->useDefaultSchemaGrammar();
}

$builder = new MySqlBuilder($this);
$builder->blueprintResolver(function ($table, $callback) {
return new Blueprint($table, $callback);
});

return $builder;
}
}

我的包裹有服务提供商:
./packages/vendorname/packagename/src/VendornamePackagenameServiceProvider.php 注册()方法:

public function register()
{ if ($this->app->config->get('vendorname-packagename') === null) {
$this->app->config->set('vendorname-packagename', require __DIR__.'/../config/vendorname-packagename');
}

$this->app->bind('db.connection.mysql', Vendorname\Packagename\Extension\MySqlConnection::class);
}

我的包也有迁移文件:
./packages/vanilla/vanilla-admin/src/migrations/2018_02_07_213437_create_logs_table.php :

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
//use Vendorname\Packagename\Extension\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateLogsTable extends Migration
{

public function up()
{
Schema::create('logs', function (Blueprint $table) {
$table->increments('id');
$table->text('package');
$table->longText('log');
$table->myMethod();
$table->timestamps();
});
}

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

我如何使用标准的“使用 Illuminate\Database\Schema\Blueprint;”在迁移中,因为我不想使用“use Vendorname\Packagename\Extension\Blueprint;”。

此外,我在运行迁移“php artisan migrate:refresh”时遇到错误:

Type error: Argument 1 passed to CreateLogsTable::{closure}() must be a
n instance of Vanilla\VanillaAdmin\Extension\Blueprint, instance of Ill
uminate\Database\Schema\Blueprint given, called in /Users/raido/.compos
er/vendor/bin/laravel-packages/vendor/laravel/framework/src/Illuminate/
Database/Schema/Builder.php on line 164

请帮我找到解决办法。我在互联网上搜索并测试了一整天,我无法让它工作:(

最佳答案

我在搜索同样的问题时发现了这个。我正在使用 Laravel 5.8 并这样做。

  • 创建自定义蓝图类
  • namespace App\Database\Schema;

    class Blueprint extends \Illuminate\Database\Schema\Blueprint
    {
    public function datetimeCreatedAt($precision = 0)
    {
    return $this->dateTime('created_at', $precision);
    }

    }
  • 创建 MySqlConnection 类或任何你需要的东西
  • namespace App\Database;

    class MySqlConnection extends \Illuminate\Database\MySqlConnection
    {
    public function getSchemaBuilder()
    {
    $builder = parent::getSchemaBuilder();
    $builder->blueprintResolver(function ($table, $callback) {
    return new \App\Database\Schema\Blueprint($table, $callback);
    });

    return $builder;
    }
    }
  • 更改 AppServiceProvider
  • namespace App\Providers;

    use App\Database\MySqlConnection;
    use Illuminate\Database\Connection;
    use Illuminate\Support\ServiceProvider;

    class AppServiceProvider extends ServiceProvider
    {
    public function register()
    {
    Connection::resolverFor('mysql', function ($connection, $database, $prefix, $config) {
    return new MySqlConnection($connection, $database, $prefix, $config);
    });
    }
    }

    关于php - 扩展 Laravel 5.5/5.6 蓝图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49113407/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com