How to use a calculated value as if it's a column value in Laravel model?
<p>I have a project that has a project model which looks like this:</p>

<pre><code>class Product extends Model
{
    public $timestamps = true;
    protected $guarded = ['id'];
    protected $table = 'products';
    protected $hidden = ['created_at', 'updated_at'];
    protected $fillable = ['name', 'category_id', 'units', 'b_price', 's_price', 'warn_count', 'added_by'];

    public function category()
    {
        return $this-&gt;belongsTo('App\Category');
    }

    public function stock(){
        $product_id = $this-&gt;id;
        $filter = ['product_id' =&gt; $product_id];
        //STOCK PLUS
        //credit purchases
        $cr_purchases = CreditPurchase::where($filter)-&gt;sum('qty');
        //purchases
        $purchases = Purchase::where($filter)-&gt;sum('qty');
        //returns in
        $re_in = ReturnIn::where($filter)-&gt;sum('qty');
        //STOCK MINUS
        //credit sales
        $cr_sales = CreditSale::where($filter)-&gt;sum('qty');
        //sales
        $sales = Sale::where($filter)-&gt;sum('qty');
        //returns out
        $re_out = ReturnOut::where($filter)-&gt;sum('qty');
        //damaged
        $damaged = DamagedProduct::where($filter)-&gt;sum('qty');
        return $cr_purchases + $purchases + $re_in - ($cr_sales + $sales + $re_out + $damaged);
    }
}
</code></pre>

<p>As can be seen stock is a calculated value for each model. I wish to make queries based on it as though it were a column of the products table.</p>