【问题标题】:Laravel 2 instances of BadMethodCallException: 'int does not exist' and 'method does not exist'Laravel 2 BadMethodCallException 实例:'int 不存在'和'方法不存在'
【发布时间】:2019-06-23 03:19:59
【问题描述】:

我目前正在通过终端在 Laravel 中进行迁移,尝试使用 php artisan migrate 时出现这两个错误;我将在下面打印错误:

 BadMethodCallException  : Method Illuminate\Database\Schema\Blueprint::int does not exist.

  at /Users/shaquilenoor/Desktop/chatapi/vendor/laravel/framework/src/Illuminate/Support/Traits/Macroable.php:100
     96|      */
     97|     public function __call($method, $parameters)
     98|     {
     99|         if (! static::hasMacro($method)) {
  > 100|             throw new BadMethodCallException(sprintf(
    101|                 'Method %s::%s does not exist.', static::class, $method
    102|             ));
    103|         }
    104| 

  Exception trace:

  1   Illuminate\Database\Schema\Blueprint::__call("int")
      /Users/shaquilenoor/Desktop/chatapi/database/migrations/2019_01_29_045824_create_contacts_table.php:15

  2   CreateContactsTable::{closure}(Object(Illuminate\Database\Schema\Blueprint))
      /Users/shaquilenoor/Desktop/chatapi/vendor/laravel/framework/src/Illuminate/Database/Schema/Builder.php:164

这两个错误似乎都源于我的 CreateContactsTable,所以我将在下面打印该文件中的代码:

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

class CreateContactsTable extends Migration
{
    public function up()
    {
        Schema::create('contacts', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('user1_id');
            $table->unsignedInteger('user2_id');
            $table->int('room_id')->unique();
            $table->timestamps();
            $table->foreign('room_id')->references('id')->on('rooms');
            $table->foreign('user1_id')->references('id')->on('users');
            $table->foreign('user2_id')->references('id')->on('users');
        });
    }

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

【问题讨论】:

    标签: php laravel terminal composer-php laravel-migrations


    【解决方案1】:

    错误在一行:

    $table->int('room_id')->unique();
    

    没有int 方法。你应该改用integer

    $table->integer('room_id')->unique();
    

    【讨论】:

    • 谢谢!该错误消失了,但现在我有一个新问题,我将在下面打印: PDOException::("SQLSTATE[HY000]: General error: 3780 Reference column 'room_id' and referenced column 'id' in foreign key constraint 'contacts_room_id_foreign' are不兼容。”)/Users/shaquilenoor/Desktop/chatapi/vendor/laravel/framework/src/Illuminate/Database/Connection.php:458 2 PDOStatement::execute() /Users/shaquilenoor/Desktop/chatapi/vendor/laravel/框架/src/Illuminate/Database/Connection.php:458
    【解决方案2】:

    $table->int 更改为$table->integer,因为int 方法不存在

    【讨论】:

    • 谢谢!该错误消失了,但现在我有一个新问题,我将在下面打印: PDOException::("SQLSTATE[HY000]: General error: 3780 Reference column 'room_id' and referenced column 'id' in foreign key constraint 'contacts_room_id_foreign' are不兼容。”)/Users/shaquilenoor/Desktop/chatapi/vendor/laravel/framework/src/Illuminate/Database/Connection.php:458 2 PDOStatement::execute() /Users/shaquilenoor/Desktop/chatapi/vendor/laravel/框架/src/Illuminate/Database/Connection.php:458
    • 嘿@Chakeeel,您将rooms 表中的id 列定义为主键还是唯一键?
    猜你喜欢
    • 2019-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-24
    相关资源
    最近更新 更多