执行Laravel Artisan migrate时触发BadMethodCallException错误求助
解决Laravel迁移报错:
Blueprint::password方法不存在 嘿,这个问题我之前也碰到过!你看到的BadMethodCallException是因为Laravel的Schema Blueprint类压根就没有password这个字段创建方法——很多新手会误以为有这个快捷方法,但其实Laravel里创建密码字段就是用普通的字符串类型来定义的。
具体修复步骤:
- 打开你的迁移文件(一般在
database/migrations/目录下,文件名类似xxxx_xx_xx_xxxxxx_create_users_table.php) - 找到代码里写了
$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
相关产品推荐
相关产品推荐

