vendredi 7 décembre 2018

Why I can not insert data in related table?

Code is:

$product = ProductModel::create($data);


           if ($request->filled('Price')) {
                $productPrice = new ProductPriceModel;
                $productPrice->Price = $request->Price;
                $productPrice->Country_Id = 1;
                $productPrice->City_Id = 1;
                // I can pass here id of product $productPrice->Products_Id = $product->Id
                $product->ProductPrice()->save($productPrice);
            }

ProductModel is:

 public function ProductPrice()
    {

        return $this->hasMany('App\ProductPrice', 'Products_Id', 'Id');
    }

ProductPriceModel is:

 public function Product()
    {

        return $this->belongsTo('App\Product', 'Id', 'Products_Id');
    }

When I try to add price after create product I get this error:

PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'Products_Id' cannot be null in



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire