SQLSTATE[42S22]:未找到列:1054 'field list'

时间:2021-04-16 21:39:07

标签: php laravel

我在尝试 php artisan db:seed 时收到错误

<块引用>

Illuminate\Database\QueryException

SQLSTATE[42S22]:未找到列:1054 未知列 “字段列表”中的“short_description”(SQL:插入categories (name, slug, short_description, description, regular_price, SKUstock_statusquantityimagecategory_idupdated_at, created_at) 值(atque dicta aut voluptatum, atque-dicta-aut-voluptatum, Id repelt nesciunt sat omnis et nihil 麦格南Incidunt a voluptatum praesentium tempore non et nesciunt。 Saepe iste qui voluptatem。 Rerum nobis voluptatibus sint voluptatibus., Nulla modi quos porro numquam eius ullam sed。广告 voluptatibus quia velit。 Voluptas iure molestiae veniam qui。 Eos et qui quod nemo repudiandae fugit eos esse。无所不能 最大的区别。 Est quia fugiat tenetur eum aspernatur 区别。 Rem excepturi magnam quaerat dolorem et adipisci tenetur。 Temporibus quasi possimus 坐 exercitationem qui。奎维尔波罗 praesentium delectus reprehenderit enim similique。 quis quos et cum commodi nostrum., 330, DIGI378, instock, 158, digital_2, 2, 2021-04-16 21:12:05, 2021-04-16 21:12:05))

类别模型

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    use HasFactory;

    protected $table = "categories";
}

产品型号

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    use HasFactory;

    protected $table = "products";
}

产品架构 (2021_04_02_190519_create_products_table.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->string('name');
            $table->string('slug')->unique();
            $table->string('short_description')->nullable();
            $table->text('description');
            $table->decimal('reqular_price');
            $table->decimal('sales_price')->nullable();
            $table->string('SKU');
            $table->enum('stock_status',['instock','outofstock']);
            $table->boolean('featured')->default(false);
            $table->unsignedInteger('quantity')->default(10);
            $table->string('image')->nullable();
            $table->text('images')->nullable();
            $table->bigInteger('category_id')->unsigned()->nullabe();

            $table->timestamps();
            $table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');
        });
    }

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

类别架构 (2021_04_02_212544_create_categories_table.php)

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

class CreateCategoriesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('categories', function (Blueprint $table) {
            $table->id();
            $table->string('name')->unique();
            $table->string('slug')->unique();
            $table->timestamps();
        });
    }

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

产品工厂

namespace Database\Factories;

use App\Models\Category;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;

class ProductFactory extends Factory
{
    /**
     * The name of the factory's corresponding model.
     *
     * @var string
     */
    protected $model = Category::class;

    /**
     * Define the model's default state.
     *
     * @return array
     */
    public function definition()
    {
        $product_name = $this->faker->unique()->words($nb=4,$asText=true);

        $slug = Str::slug($product_name);

        return [
            'name' => $product_name,
            'slug' => $slug,
            'short_description' => $this->faker->text(200),
            'description' => $this->faker->text(500),
            'regular_price' => $this->faker->numberBetween(10, 500),
            'SKU' => 'DIGI'.$this->faker->unique()->numberBetween(100, 500),
            'stock_status' => 'instock',
            'quantity' => $this->faker->numberBetween(100, 200),
            'image' => 'digital_'.$this->faker->unique()->numberBetween(1, 22),
            'category_id' => $this->faker->numberBetween(1, 5),
        ];
    }
}

品类工厂

namespace Database\Factories;

use App\Models\Category;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;

class CategoryFactory extends Factory
{
    /**
     * The name of the factory's corresponding model.
     *
     * @var string
     */
    protected $model = Category::class;

    /**
     * Define the model's default state.
     *
     * @return array
     */
    public function definition()
    {
        $category_name = $this->faker->unique()->words($nb = 2, $asText = true);

        $slug = Str::slug($category_name);

        return [
            'name'=>$category_name,
            'slug'=>$slug,
        ];
    }
}

1 个答案:

答案 0 :(得分:3)

ProductFactory 上,更改:

protected $model = Category::class;

到,

protected $model = Product::class;