Support the ongoing development of Laravel.io →
Database Eloquent

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 ?

Last updated 3 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

reshadman reshadman Joined 15 Feb 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.