如何使用计算值,就像它是Laravel模型中的列值一样?

时间:2016-09-06 17:46:14

标签: php laravel

我有一个项目模型如下:

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->belongsTo('App\Category');
    }

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

可以看出,库存是每个模型的计算值。我希望基于它进行查询,就好像它是产品表的一列。

2 个答案:

答案 0 :(得分:1)

方法1
将stock方法更改为Laravel模型访问器。

public function getStockAttribute(){
   //code logic
}

将结果作为集合获取并对'stock;执行过滤;属性 我会做类似的事。

Products::where('product','like','miraa') //where
->get()
->filter(function($item) {
    return $item->stock > 100;
});

了解有关过滤collections

的信息

方法2
使用动态查询范围 请参阅laravel中的scopes

public function scopeAvailbaleStock($query, $type)
{
    return $query->where('type', $type);
    // could perform filters here for the query above
}

使用范围

获取
$users = Products::available_stock()->get();

方法3 我看到了这个包jarektkaczyk/eloquence

public function scopeWhereStock($query, $price, $operator = '=', $bool = 'and'){
    $query->where('info1', $operator, $price, $bool);
   }

// then
Products::whereStock(25); // where('info1', 25);
Products::whereStcok(25, '>'); // where('info1', '>', 25);
Products::whereStock(25, '=', 'or'); // orWhere('info1', 25);

然而,我建议使用方法1或2.第3种解决方案有效,但不确定它是否是最好的

答案 1 :(得分:0)

我通过覆盖模型上的$ append并使用stock字段的访问器来解决它。所以模型现在看起来像:

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'];
    protected $appends = ['stock'];

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

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

我在@samueldervis上建立了这个解决方案。这里可以看到一个简单的例子:http://laraveldaily.com/why-use-appends-with-accessors-in-eloquent/