i want to store multiple checkbox value by using
attach(
) method.But the problem is:
Column not found: 1054 Unknown column 'category_id' in 'field list' (SQL: insert into
size_products
(category_id
,product_id
) values (small, 5))
My Table size_products
Structure like this (size_id
, product_id
) .But dont know where came from category_id
column
Here is My Models:
//Size Model
class Size extends Model
{
public function Products()
{
return $this->belongsToMany(Product::class, 'size_products','product_id','size_id');
}
}
/////// Product Model
class Product extends Model
{
public function Categories()
{
return $this->belongsToMany(Category::class, 'category_products');
}
public function Colors()
{
return $this->belongsToMany(Category::class, 'color_products');
}
public function Sizes()
{
return $this->belongsToMany(Category::class, 'size_products');
}
public function FirstImage(){
$data = explode(',',$this->images);
return $data[0];
}
}
Here is my Controller :
$product = new Product();
$product->name= $request->input('name');
$product->model= $request->input('model');
$product->save();
$product->Categories()->attach($request->input('categories_id'));
foreach ($request->sizes as $size){
$product->Sizes()->attach($size);
}
foreach ($request->colors as $color){
$product->Colors()->attach($color);
}
Database :
Schema::create('size_products', function (Blueprint $table) {
$table->increments('id');
$table->integer('product_id')->unsigned();
$table->foreign('product_id')->references('id')->on('products')->onUpdate('cascade')->onDelete('cascade');
$table->integer('size_id')->unsigned();
$table->foreign('size_id')->references('id')->on('sizes')->onUpdate('cascade')->onDelete('cascade');
$table->timestamps();
});