【问题标题】:Why does php migration keeps on creating columns on default cashier model (User) even changed to different model为什么php迁移继续在默认收银员模型(用户)上创建列,甚至更改为不同的模型
【发布时间】:2019-12-25 05:46:43
【问题描述】:

我想将默认收银员模型更改为我的自定义模型(用户到公司)

我所做的是

  1. 将 services.php 的模型更改为 App\Models\Companies\Companies::class
  2. 将我的 .env 更改为 CASHIER_MODEL=App\Models\Companies\Companies
  3. 已发布收银员迁移并将表名更改为公司,但

php artisan migrate 仍然会更新收银员的默认模型,即用户

// services.php
    'stripe' => [
            'model' => App\Models\Companies\Companies::class,
            'key' => env('STRIPE_KEY'),
            'secret' => env('STRIPE_SECRET'),
            'webhook' => [
                'secret' => env('STRIPE_WEBHOOK_SECRET'),
                'tolerance' => env('STRIPE_WEBHOOK_TOLERANCE', 300),
            ],
        ],

CASHIER_MODEL=App\Models\Companies\Companies  // on .env


// on migration
    public function up()
    {
        Schema::table('companies', function (Blueprint $table) {
            $table->string('stripe_id')->nullable()->collation('utf8mb4_bin')->index();
            $table->string('card_brand')->nullable();
            $table->string('card_last_four', 4)->nullable();
            $table->timestamp('trial_ends_at')->nullable();
        });
    }

【问题讨论】:

  • 你试过了吗?取消 artisan serve 并运行 composer dump-autoload 并再次运行 php artisan serve。

标签: php laravel stripe-payments laravel-cashier


【解决方案1】:

即使您已将 Cashier 迁移发布到 database/migrations 文件夹并对其进行了编辑,Laravel 也会继续从供应商文件夹加载它们。

解决方案是明确告诉 Laravel 忽略供应商迁移。

在AppServiceProvider的注册方法中添加如下内容:

// Use the Cashier migrations from migration folder instead of vendor folder
Cashier::ignoreMigrations();

记得还要包括:

use Laravel\Cashier\Cashier;

【讨论】:

    【解决方案2】:

    老实说,我认为这是 Laravel Cashier 错误。默认的 Laravel Cashier 迁移应该创建订阅表,加入您在 Cashier 配置中定义的 Cashier 模型,并且它应该只将 Cashier 列添加到该模型。无论如何,这是完整的解决方法。

    1. 通过将 Cashier::ignoreMigrations(); 添加到 register() 方法来禁用 AppServiceProvider.php 中的默认 Laravel Cashier 迁移。

      namespace App\Providers;
      
      use Laravel\Cashier\Cashier;
      use Illuminate\Support\ServiceProvider;
      
      class AppServiceProvider extends ServiceProvider
      {
          /**
           * Bootstrap any application services.
           *
           * @return void
           */
          public function boot()
          {
              //
          }
      
          /**
           * Register any application services.
           *
           * @return void
           */
          public function register()
          {
              Cashier::ignoreMigrations();
          }
      }
      
    2. 创建以下迁移:php artisan make:migration create_subscriptions_tablephp artisan make:migration create_customer_columns

    3. vendor/laravel/cashier/database/migrations/ 中迁移的迁移内容复制到新创建的迁移文件中。

    4. 将所有对 usersuser_id 的引用替换为您决定使用的 Laravel Cashier 模型。

    【讨论】:

      猜你喜欢
      • 2021-11-09
      • 1970-01-01
      • 2016-10-25
      • 1970-01-01
      • 2017-10-08
      • 2020-08-17
      • 2016-08-04
      • 2019-12-16
      • 2021-10-06
      相关资源
      最近更新 更多