[Laravel5] Bermain-main dengan Pivot

kondisi:

table order dan table produk memiliki relasi many to many.

selain relasi tersebut, dibutuhkan order quantiti.

tahapan persiapan:

1. membuat table order_product dengan coloum:

[order_id][product_id][quantiti]

2. membuat relation belongHasMany pada kedua model:

return $this->belongsToMany(“App\Product”)->withPivot(“quantity”);//pada order, begitupun pda Product.

penggunaan:

$product = App\Product::find(1); //mengambil produk dulu

$order = App\Order::create();

$order->products()->attach($produk);

 $order->products->find(1)->pivot->quantity=12;

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s