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