-2

when I want to attach the categories() to productTableSeeder :

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'product_name' in 'field list' (SQL: insert into category_product (category_id, product_name) values (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');
    }
}

products

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

categories

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

I want each product has 2 categories for example soccer player/(Soccer/Sport)

but it said peoduct_name column is unknown column

Tharaka Dilshan
  • 4,371
  • 3
  • 14
  • 28
hamidprog2003
  • 1
  • 1
  • 1
  • 3
  • There is a syntax error in your `Product::create()` function with a missing comma and an apostrophe. – Tharaka Dilshan Aug 04 '19 at 17:04
  • Please in code questions give a [mre]--cut & paste & runnable code; example input with desired & actual output (including verbatim error messages); tags; clear specification & explanation. That includes the least code you can give that is code that you show is OK extended by code that you show is not OK. (Debugging fundamental.) PS You have a syntax error. Read the grammar & manual. Show that constituent subexpressions are OK. Make it clear that your question is about *that error* & ask re your overall goal later in a new post. PS Clearly there is non-minimal code/data here. – philipxy Aug 05 '19 at 07:59

2 Answers2

1

i looked your code, i found one small thing is missing. That is you forgot to end single quote in ProductTableSeeder class in description.

Mitesh Rathod
  • 879
  • 5
  • 18
0

Your ProductsTableSeeder has an issue, its fixed here:

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);
 }
}
Zadat Olayinka
  • 441
  • 3
  • 8