找不到列:1054“字段列表”中的未知列“product_name””)

问题描述 投票:0回答:2

当我想将categories()附加到productTableSeeder时:

SQLSTATE[42S22]:未找到列:1054 中的未知列“product_name” '字段列表'(SQL:插入到

category_product
(
category_id
,
product_name
) 值 (1, 科纳塔拉-LG-拉拉-15658-1))

类别_产品

<?php

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

class CreateCategoryProduct extends Migration
{

    public function up()
    {
        Schema::create('category_product', function (Blueprint $table) {
            $table->bigIncrements('id');

            $table->integer('product_id')->unsigned()->nullable();
            $table->foreign('product_id')->references('id')
                ->on('products')->onDelete('CASCADE');


            $table->integer('category_id')->unsigned()->nullable();
            $table->foreign('category_id')->references('id')
                ->on('categories')->onDelete('CASCADE');


            $table->timestamps();
        });
    }

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

产品

<?php

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

class CreateProductsTable extends Migration
{

    public function up()
    {
        Schema::create('products', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('category_id')->unsigned()->nullable();
            $table->string('name')->unique();
            $table->string('slug')->unique();
            $table->string('color');
            $table->string('details')->nullable();
            $table->integer('price')->unsigned();
            $table->text('description');
            $table->string('image');
            $table->boolean('featured')->default(false);
            $table->timestamps();
        });
    }


    public function down()
    {
        Schema::dropIfExists('products');
    }
}

类别

<?php

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

class CreateCategoriesTable extends Migration
{

    public function up()
    {
        Schema::create('categories', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name')->unique();
            $table->string('slug')->unique();
            $table->timestamps();
        });
    }


    public function down()
    {
        Schema::dropIfExists('categories');
    }
}

类别.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{

    public function products()
    {
        return $this->belongsToMany('App\Product');
    }

}

产品.php

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;


class Product extends Authenticatable
{

    protected $fillable =
        ['name','slug','description','details','price','image','color'];


    public function categories()
    {
        return $this->belongsToMany('App\Category');
    }

    public function scopeMightAlsoLike($query)
    {
        return $query->inRandomOrder()->take(4);

    }

    public function setNameAttribute($value)
    {
        $this->attributes['name'] = str_replace(' ','-',$value);
    }

    protected $primaryKey = 'name';
    public $incrementing = false;
    protected $keyType = 'string';
    protected $table = 'products';
}

产品TableSeeder

<?php

use App\Product;
use Illuminate\Database\Seeder;

class ProductsTableSeeder extends Seeder
{

    public function run()
    {

        for ($i=1; $i <= 30; $i++) {
            Product::create([
                'name' => 'کنترل LG مدل 15658 '.$i,
                'slug' => 'kep-15658u'.$i,
                'details' => [13,14,15][array_rand([13,14,15])] . ' inch, ',

                'price' => rand(25, 500000),
                'description' =>'Lorem '. $i .'consectetur adipisicingvoluptas 
                 unde as
                'image' => 'کنترل LG مدل RM-L1162.jpg',
                'color' => 'red'
            ])->categories()->attach(1);
        }


        $product = Product::find(1);
        $product->categories()->attach(2);
    }
 }

我希望每个产品都有 2 个类别,例如足球运动员/(足球/运动)

但它说 peoduct_name 列是未知列

php laravel foreign-keys migration laravel-5.8
2个回答
1
投票

我查看了你的代码,发现缺少一件小东西。 那是您忘记在描述中的 ProductTableSeeder 类中结束单引号。


0
投票

您的

ProductsTableSeeder
有问题,已在此处修复:

use App\Product;
use Illuminate\Database\Seeder;

class ProductsTableSeeder extends Seeder
{

public function run()
{

    for ($i=1; $i <= 30; $i++) {
        Product::create([
            'name' => 'کنترل LG مدل 15658 '.$i,
            'slug' => 'kep-15658u'.$i,
            'details' => [13,14,15][array_rand([13,14,15])] . ' inch, ',
            'price' => rand(25, 500000),
            'description' =>'Lorem '. $i .'consectetur adipisicingvoluptas 
             unde as',
            'image' => 'کنترل LG مدل RM-L1162.jpg',
            'color' => 'red'
        ])->categories()->attach(1);
    }


    $product = Product::find(1);
    $product->categories()->attach(2);
 }
}
© www.soinside.com 2019 - 2024. All rights reserved.