You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel项目数据填充报错:表menus不存在但迁移已完成

Laravel数据填充报错:表'menus'不存在的解决方法

执行Laravel数据库填充时,终端持续报错提示restaurant_ordering_website.menus表不存在,但实际数据库中创建的表为menu。已检查项目文件命名、尝试重命名模型/工厂/迁移及填充文件,且迁移状态显示所有迁移均已执行,问题仍未解决。

错误信息

Illuminate\Database\QueryException 

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'restaurant_ordering_website.menus' doesn't exist (Connection: mysql, SQL: insert into `menus` (`item_name`, `price`, `description`, `updated_at`, `created_at`) values (Magali Rosenbaum, 14.72, Pariatur natus officiis in impedit magni dolore neque fuga adipisci natus enim., 2023-02-24 04:15:08, 2023-02-24 04:15:08))

迁移状态

Migration name .................................................................................................................... Batch / Status  
  2014_10_12_000000_create_users_table ..................................................................................................... [1] Ran  
  2019_12_14_000001_create_personal_access_tokens_table .................................................................................... [1] Ran  
  2023_02_22_100010_menu_table ............................................................................................................. [1] Ran  
  2023_02_22_100024_orderitems_table ....................................................................................................... [1] Ran  
  2023_02_22_100035_order_table ............................................................................................................ [1] Ran 

相关文件代码

DatabaseSeeder.php

<?php

namespace Database\Seeders;

// use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use App\Models\Menu;
use App\Models\Order;
use App\Models\Orderitems;
use Illuminate\Database\Seeder;
use \App\Models\User;
class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     */
    public function run(): void
    { 
        User::factory(10)->create();
        Menu::factory(10)->create();
        Orderitems::factory(10)->create();
        Order::factory(10)->create();

        // \App\Models\User::factory()->create([
        //     'name' => 'Test User',
        //     'email' => 'test@example.com',
        // ]);
    }
}
<?php

namespace Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;

/**
 * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Menu>
 */
class MenuFactory extends Factory
{
    /**
     * Define the model's default state.
     *
     * @return array<string, mixed>
     */
    public function definition(): array
    {
        return [
            //
            'item_name'=>fake()->name(),
            'price'=>fake()->randomFloat(2, 5, 15),
            'description'=>fake()->sentence(10),
        ];
    }
}
<?php

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

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        //
        Schema::create('menu', function (Blueprint $table) {
            $table->id();
            $table->string("item_name");
            $table->float("price", 8, 2);
            $table->string("description");
        });
    }

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

问题原因与解决方法

核心原因

Laravel Eloquent模型默认会将模型名的复数形式作为数据表名。你的模型为Menu,框架自动尝试访问复数表menus,但迁移文件中创建的是单数表menu,导致表名不匹配。

解决方式(二选一即可)

  1. 修改模型指定表名
    在App\Models\Menu模型中添加$table属性,明确指定对应的数据表:
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Menu extends Model
{
    // 指定表名为单数的menu
    protected $table = 'menu';
}
  1. 修改迁移文件创建复数表名
    若想遵循Laravel表名惯例(复数形式),可修改迁移文件将表名改为menus,再重新执行迁移:
  • 回滚迁移:
php artisan migrate:rollback
  • 修改迁移文件的up和down方法:
public function up(): void
{
    Schema::create('menus', function (Blueprint $table) {
        $table->id();
        $table->string("item_name");
        $table->float("price", 8, 2);
        $table->string("description");
        // 补充自动时间戳字段,匹配填充时生成的created_at/updated_at
        $table->timestamps();
    });
}

public function down(): void
{
    Schema::dropIfExists('menus');
}
  • 重新执行迁移与填充:
php artisan migrate
php artisan db:seed

注意:原迁移文件未添加timestamps()方法,但填充时自动生成了created_at和updated_at字段,无论选择哪种方案,都建议在迁移的up方法中添加该字段,避免后续出现字段不存在的问题。

内容的提问来源于stack exchange,提问作者Tharshen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 23:54:32