php - laravel accessors in controller? -
i've add accessors , mutators in laravel model.
public function getamountattribute($amount) { return ($amount) / 100; } public function setamountattribute($amount) { $this->attributes['amount'] = $amount * 100; }
these working fine me. @ 1 place facing issue : issue want apply aggregate function in controller amount field. accessors not working.
model::with(['xxx'])->where('parent_id', $id)->sum('amount')
value of $id fine checked. it's giving me sum of amount field parent_id equals $id. should give results after diving 100.
thanks in advance.
accessors , mutators allow format eloquent attributes when retrieving them model or setting value.
... not while querying table.
model::with(['xxx']) ->addselect(db::raw('sum(`table`.amount/100) sum_of_amounts')) ->where('parent_id', $id) ->get();
Comments
Post a Comment