You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

执行Laravel Artisan migrate时触发BadMethodCallException错误求助

解决Laravel迁移报错:Blueprint::password方法不存在

嘿,这个问题我之前也碰到过!你看到的BadMethodCallException是因为Laravel的Schema Blueprint类压根就没有password这个字段创建方法——很多新手会误以为有这个快捷方法,但其实Laravel里创建密码字段就是用普通的字符串类型来定义的。

具体修复步骤:

  1. 打开你的迁移文件(一般在database/migrations/目录下,文件名类似xxxx_xx_xx_xxxxxx_create_users_table.php)
  2. 找到代码里写了$table->password()的那一行,把它替换成字符串类型的定义:
    • 如果你想保留默认长度,可以写:$table->string('password')
    • 更推荐的写法是$table->string('password', 60),因为Laravel默认用bcrypt加密后的密码长度是60,这样更节省数据库空间

错误代码示例:

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->id();
        $table->string('name');
        $table->string('email')->unique();
        $table->password(); // 这行就是报错的根源
        $table->timestamps();
    });
}

修复后的正确代码:

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->id();
        $table->string('name');
        $table->string('email')->unique();
        $table->string('password', 60); // 替换成这行
        $table->timestamps();
    });
}

替换完成后,重新执行php artisan migrate,这个错误就应该消失啦!

内容的提问来源于stack exchange,提问作者KB_Shayan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 08:32:15