I have two tables
products (model: Products)
product_common_auction (model: ProductCommonAuction)
Each Product can have one record on ProductCommonAuction (hasone() in Product models)
Each ProductCommonAuction belongs to one row in Porduct
When I want to save a model I use the following code to save a product with it's relation:
$Product = new Products();
$Product->title = $toSave['auction_title'];
$Product->body = $toSave['auction_description'];
$Product->price = 50000;
$Product->product_type_id = 1;
$Product->sku = time();
$Product->enabled = ((!empty($toSave['enabled']))?1:0);
$Product->approved = 1;
$Product->save();
$Common = new ProductCommonAuction();
$Common->product_id = $Product->id;
$Common->save();
I also can get results with this:
$Product = new Products();
$Product->title = $toSave['auction_title'];
$Product->body = $toSave['auction_description'];
$Product->price = 50000;
$Product->product_type_id = 1;
$Product->sku = time();#to do
$Product->enabled = ((!empty($toSave['enabled']))?1:0);
$Product->approved = 1;
$Product->save();
$Product->common_auction()->save(new ProductCommonAuction);
Which one is better ?
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community