Skip to content

Instantly share code, notes, and snippets.

@aklimaruhina
Created December 24, 2022 05:01
Show Gist options
  • Save aklimaruhina/b2275af23b0475ab0e0e6737fe261a3a to your computer and use it in GitHub Desktop.
Save aklimaruhina/b2275af23b0475ab0e0e6737fe261a3a to your computer and use it in GitHub Desktop.
step1: need to show total price of products
add appends to your Order model
1. add this on top protected $appends = ['total_purchase_price', 'update_url'];
2. add functions
// This is another models
public function orderItems() {
return $this->hasMany('App\OrderItem', 'order_id');
}
public function getTotalPurchasePriceAttribute($value)
{
$sum = 0;
foreach($this->orderItems as $order_item){
if(isset($order_item->purchase_price)){
$sum += ($order_item->purchase_price * $order_item->qty);
}else{
$sum += ($order_item->price * $order_item->qty);
}
}
return $sum;
} //
In view file call like this
$order->total_purchase_price
3. to get url simply add this in your model
public function getUpdateUrlAttribute()
{
return route('route_name',$this->id);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment