This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.
I used RelationBehavior to get some pivot data, and I want to calculate two columns of that pivot data. So how can I do it. Example: Three columns below are pivot: Product price Discount Sell price = Product price * discount
Yes, see this docs page https://octobercms.com/docs/database/relations#many-to-many
under the "Retrieving intermediate table columns" section.
You can define an accessor method to calculate the sum.
Just to be sure, do you want the sum as a column in the records list view or as a field in the record create/update form?
I want to sum columns in Form, but Form in Relation popup. In normal model, I used dependsOn and filterFields to update a fields from another field, so can I do that in Relation popup? I tried filterFields in pivot model but don't work
pivot[based_price]:
label: Giá bán lẻ
type: number
defaultFrom: price
readOnly: true
pivot[discount]:
label: Chiết khấu (%)
type: number
span: auto
default: 0
pivot[quantity]:
label: Số lượng
type: number
span: auto
default: 1
min: 1
pivot[sell_price]:
label: Giá bán
type: number
defaultFrom:
pivot[based_price] * pivot[discount]
hidden: 1
dependsOn:
- pivot[discount]
I want sell_price will be calculate from based_price and discount (based_price is fixed and discount is filled by user)
Last updated
Maybe use this to manipulate the pivot widget would work:
https://octobercms.com/docs/backend/relations#extend-pivot-widget
class OrderProductPivot extends Pivot {
public function setSellPriceAttribute()
{
$this->attributes['sell_price'] = $this->based_price * ( (100 - $this->discount) / 100) ;
}
}
Hi @mjauvin, thanks for your supporting. I have tried to use set attribute function inside pivot model and set dependsOn in pivot form. And it works according to my expectations
Last updated
1-12 of 12