I have 3 tables:
stations (id,station_name)
products (id,product_name)
product_station (station_id,product_id)
Station Model
protected $fillable = ['station_name'];
public function products(){
return $this->belongsToMany(Product::class);
}
Product Model
protected $fillable = ['product_name'];
public function stations(){
return $this->belongsToMany(Station::class);
}
I already have inserted stations and products and i want to insert station with it's own product using into Pivot table
AdminProductStationcontroller
public function create()
{
$station = Station::pluck('station_name','id')->all();
$products = Product::pluck('product_name','id')->all();
return view('admin.product_stations.create',compact('station','products'));
}
I think there is error in store function below
public function store(Request $request)
{
$station = new Station();
$product = new Product();
$station->save();
$station->products()->attach($product);
return redirect('/admin/product_stations');
}
So i have got this error
(2/2) QueryException
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'product_id' cannot be null (SQL: insert into product_station
(product_id
, station_id
) values (, 25))