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

时间:2019-08-04 16:55:03

标签: laravel foreign-keys migration relation laravel-5.8

当我要将category()附加到productTableSeeder时:

  

SQLSTATE [42S22]:找不到列:1054中的未知列'product_name'         “字段列表”(SQL:插入category_product          (category_idproduct_name)值(1,کنترل-LG-مدل-15658-1))

category_product

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

Category.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{

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

}

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

ProductTableSeeder

<?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列是未知列

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分: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);
 }
}