yyykk00
@yyykk00

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

Laravel table作成のエラー 初学者

Laravel table作成のエラー

Laravel学習初学者です。
Laravelで商品一覧画面を作成しています。
マイグレーションファイルを作成し、tableのコードを編集して php artisan migrateしたところ、エラーが発生しました。
解決方法のヒントなどご教示いただけると幸いです。

発生している問題・エラー (ターミナル)

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

  at /Applications/MAMP/htdocs/step7/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| 

  Exception trace:

  1   Illuminate\Database\Schema\Blueprint::__call("id", [])
      /Applications/MAMP/htdocs/step7/database/migrations/2023_07_26_231216_create_products_table.php:17

  2   CreateProductsTable::{closure}(Object(Illuminate\Database\Schema\Blueprint))
      /Applications/MAMP/htdocs/step7/vendor/laravel/framework/src/Illuminate/Database/Schema/Builder.php:166

該当するソースコード

以下のテーブルを編集してマイグレーションを実行しようとしたところ、上記のエラーが出てしまいます。
$table->id();の記述がなければ実行することができるのですが、記述するとエラーが出てしまいます。

<?php

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

class CreateProductsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('products', function (Blueprint $table) {
            $table->id();
            $table->bigIncrements('company_id');
            $table->string('product_name');
            $table->integer('price');
            $table->integer('stock');
            $table->string('comment')->nullable();
            $table->text('img_path')->nullable();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('products');
    }
}

自分で試したこと

composerのアップデート

プログラミング始めたばかりなのでご教授お願いします

0

2Answer

もしかしたらLaravelのバージョンが原因かもしれません。

xxx->id();

は7.x移行のバージョンで利用可能とあります。

1Like

Comments

  1. @yyykk00

    Questioner

    コメントいただきありがとうございます。
    いただいた回答で疑問に思っていたことが解決しました!!
    ご教授いただきありがとうございました。

Comments

  1. @yyykk00

    Questioner

    コメントいただきありがとうございます。
    お送りいただいた内容で解決いたしました!
    ご教授いただきありがとうございました。

Your answer might help someone💌