Laravel 2 BadMethodCallException实例:“ int不存在”和“方法不存在”

时间:2019-01-29 22:20:48

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

我目前正在通过终端在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');
    }
}

2 个答案:

答案 0 :(得分:1)

错误所在:

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

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

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

答案 1 :(得分:0)

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