Skip to content

Instantly share code, notes, and snippets.

@korrio
Created October 20, 2015 14:02
Show Gist options
  • Save korrio/ec3680af95f98afec000 to your computer and use it in GitHub Desktop.
Save korrio/ec3680af95f98afec000 to your computer and use it in GitHub Desktop.
Order Model
<?php
class Order
extends Eloquent
{
protected $table = "order";
protected $guarded = ["id"];
protected $softDelete = true;
public function account()
{
return $this->belongsTo("Account");
}
public function orderItems()
{
return $this->hasMany("OrderItem");
}
public function products()
{
return $this->belongsToMany("Product", "order_item");
}
public function getTotalAttribute()
{
$total = 0;
foreach ($this->orderItems as $orderItem)
{
$total += $orderItem->price * $orderItem->quantity;
}
return $total;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment