【问题标题】:Can't create unsigned column in Laravel 7无法在 Laravel 7 中创建无符号列
【发布时间】:2020-09-26 06:09:31
【问题描述】:

我正在使用 laravel 构建管理应用程序。我正在尝试在 laravel 中创建表“role_user”,但是当我运行“php artisan migrate”命令时,我得到以下信息:BadMethodCallException 方法 Illuminate\Database\Schema\Blueprint::name 不存在。谁能告诉你这个错误是什么意思?


use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateRolesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('roles', function (Blueprint $table) {
            $table->id();
            $table->name();
            $table->timestamps();
        });
    }

但如果我想迁移,我会看到

λ php artisan migrate
Migrating: 2020_06_07_055653_create_roles_table

   BadMethodCallException

  Method Illuminate\Database\Schema\Blueprint::name does not exist.

  at E:\laragon\www\os\vendor\laravel\framework\src\Illuminate\Support\Traits\Macroable.php:103
     99|      */
    100|     public function __call($method, $parameters)
    101|     {
    102|         if (! static::hasMacro($method)) {
  > 103|             throw new BadMethodCallException(sprintf(
    104|                 'Method %s::%s does not exist.', static::class, $method
    105|             ));
    106|         }
    107|

  • Bad Method Call: Did you mean Illuminate\Database\Schema\Blueprint::rename() ?

  1   E:\laragon\www\os\database\migrations\2020_06_07_055653_create_roles_table.php:18
      Illuminate\Database\Schema\Blueprint::__call("name", [])

  2   E:\laragon\www\os\vendor\laravel\framework\src\Illuminate\Database\Schema\Builder.php:166
      CreateRolesTable::{closure}(Object(Illuminate\Database\Schema\Blueprint))

【问题讨论】:

  • 我是漂亮的用户,你不需要 $table->id();打电话
  • 好的,我删除了 $table->id();但还是有这个问题
  • 这不是正确的迁移文件,文件的类是CreateRoleUserTable,所以你的迁移文件命名为create_role_user_table,而你的错误来自create_roles_table
  • 谢谢,修复它。
  • $table->name(); 不存在,可能是$table->string('name'); laravel.com/docs/7.x/migrations#creating-columns

标签: php laravel laravel-7


【解决方案1】:

您可以将代码更改为:

    Schema::create('roles', function (Blueprint $table) {
        $table->id();
        $table->string('name');
        $table->timestamps();
    });

【讨论】:

    【解决方案2】:

    改为:

    $table->name();
    

    应该是这样的:

    $table->string('name', 32); // change 32 to max length
    

    为什么?

    在 MySQL(或任何其他数据库引擎)中没有“名称”type。您可能因为->id()->timestamps() 而被误导。也没有“id”列类型,但这很有效,因为在 Laravel 中这是作为快捷方式(因为它经常被使用)。

    所以如果你使用没有区别:

    $table->id().
    $table->timestamps();
    

    或者

    $table->bigIncrements('id');
    $table->timestamp('created_at', $precision)->nullable();
    $table->timestamp('updated_at', $precision)->nullable();
    

    因为无论如何它都在做同样的事情。

    Read more in docs.

    【讨论】:

      【解决方案3】:

      改变

      $table->id()
      

      $table->bigIncrements('id');
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-12-24
        • 2018-05-24
        • 1970-01-01
        • 2015-03-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多