我无法将数据种子到我的Laravel项目数据库中。

huangapple go评论62阅读模式
英文:

I cant seed my data into my database on my Laravel Project

问题

抱歉,你的代码片段中包含了一些 HTML 转义字符,这些字符不是有效的代码或文本。请提供不包含 HTML 转义字符的代码片段,我将为你提供翻译。

英文:

So when i try to seed my database, i keep getting this error message in my terminal. It says my table doesnt exist but its named as menus. I searched in my project folder if mistakenly named anything but nothing showed up. I have tried renaming my files, models, factories, migration and seed files yet still the same result. It keeps saying my table doesnt exist even though my migration status is all good even.

This is the error message

 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))

This is my migration status

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 

This is my databaseSeeder file

<?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',
        // ]);
    }
}

My menu factory

<?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),
        ];
    }
}

This is Menu migration file

<?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');
    }
};

答案1

得分: 0

你可以运行回滚命令来从数据库中删除"menu"表

php artisan migrate:rollback
--path="database/migrations/2023_02_22_100010_menu_table.php"

然后编辑2023_02_22_100010_menu_table.php文件,将表名"menu"更改为"menus"

Schema::create('menus', function (Blueprint $table) {
    $table->id();
    $table->string("item_name");
    $table->float("price", 8, 2);
    $table->string("description");
});

还要修改down方法

Schema::dropIfExists('menus');

然后再次运行迁移命令

php artisan migrate

现在你可以进行数据填充

php artisan db:seed

英文:

You may run the rollback command to delete the "menu" table from Database

> php artisan migrate:rollback
> --path="database/migrations/2023_02_22_100010_menu_table.php"

then edit the 2023_02_22_100010_menu_table.php file to change the table "name" menu to "menus"

Schema::create('menus', function (Blueprint $table) {
        $table->id();
        $table->string("item_name");
        $table->float("price", 8, 2);
        $table->string("description");
    });

also to down method

Schema::dropIfExists('menus');

run again migration command

php artisan migrate

Now you may seed

php artisan db:seed

huangapple
  • 本文由 发表于 2023年2月24日 12:25:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/75552632.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定