0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

[laravel9]一覧画面に遷移しようとするとエラーになってしまう

Posted at

はじめに

laravelでWEBアプリを作成中なのだが、エラーが発生し、ページへ遷移することができない。
作成ページへは遷移が可能な状況

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'laravel.meal_data' doesn't exist

エラー文は下記

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'laravel.meal_data' doesn't exist

要約すると、
データベース内に存在しないテーブルにアクセスしようとしているため、発生しています。 特に、 MealData モデルが使用している meal_data テーブルが存在しないために発生している可能性が高い。
とのこと。

テーブルはmeal_datasテーブルを作成しているので、meal_dataテーブルでないと起こられている様子。

作成の際のコマンドは以下。

php artisan make:migration create_meal_datas_table

マイグレーションファイルは以下。

public function up()
    {
        Schema::create('meal_datas', function (Blueprint $table) {
            $table->id();
            $table->string('meal')->comment('食品名');
            $table->float('amount', 8, 2)->comment('総量');
            $table->string('unit')->comment('単位');
            $table->float('calories', 8, 2)->comment('カロリー');
            $table->float('protein_gram', 8, 2)->comment('タンパク質(g)');
            $table->float('protein_calories', 8, 2)->comment('タンパク質(Kcal)');
            $table->float('fat_gram', 8, 2)->comment('脂質(g)');
            $table->float('fat_calories', 8, 2)->comment('脂質(Kcal)');
            $table->float('carbo_gram', 8, 2)->comment('炭水化物(g)');
            $table->float('carbo_calories', 8, 2)->comment('炭水化物(Kcal)');
            $table->timestamps();
        });
    }

作成自体は問題なさそう。
なんでmeal_datasではなく、meal_dataを探すのかよくわからない。。がそういうことらしい。

解決方法

Modelファイルに下記追記すれば解消される。

MealData.php
protected $table = 'meal_datas';

これで一覧画面を表示することができる。
以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?