I Try to Get result using this Model
use Illuminate\Database\Eloquent\Model;
use DB;
class Customer_Bones extends Model
{
protected $table = 'customer_bones';
protected $primaryKey = 'customer_bones_id';
protected $fillable = array(
'created_at',
'deleted',
'system_user',
'customer_id',
'bones_id'
);
public $timestamps = false;
public function getBonusCustomers(){
return $this->hasMany('App\Models\Bonus_Item','customer_bones_id');
}
public function getCustomer(){
return $this->hasOne('App\Models\Customer','customer_id');
}
}
My Controller function is this
return Customer_Bones::with(array(
'getCustomer' => function($query)
{
$query->select('customer_name','customer_id');
},
'getBonusCustomers' => function($query)
{
$query->select(DB::raw('sum(bonus_quantity) as bonusQuantity'));
}))
->get();
I want to get sum of the bonus_quantity column that belongs to Bonus_Item Table with customer_name from customer table and some other details from customer_bones table. I had Tried above method but bonusQuantity return me null.
Can I use DB::raw('sum(bonus_quantity) as bonusQuantity') inside the select clause like above to get summation of bonus_quantity column, along with other details or is there any other method?