laravel范围模型函数错误

时间:2016-03-29 16:34:20

标签: laravel laravel-blade laravel-query-builder

我正在尝试使用laravel中的范围来计算属于特定cash_sale的所有cash_sales_items。问题是,当我在这些表中有一些信息时,代码工作正常但是当表为空时我得到一个错误

Object of class Illuminate\Database\Eloquent\Builder could not be converted to string  

下面是代码和表结构

accessories_cash_salesmysql> describe accessories_cash_sales;
+---------------+------------------+------+-----+---------+----------------+
| Field         | Type             | Null | Key | Default | Extra          |
+---------------+------------------+------+-----+---------+----------------+
| id            | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| client        | varchar(255)     | NO   |     |         |                |
| exchange_rate | int(11)          | NO   |     | 0       |                |
| employee_id   | int(10) unsigned | NO   | MUL | NULL    |                |
| status        | varchar(255)     | NO   |     |         |                |
| tax           | varchar(255)     | NO   |     |         |                |
| created_at    | timestamp        | YES  |     | NULL    |                |
| updated_at    | timestamp        | YES  |     | NULL    |                |
+---------------+------------------+------+-----+---------+----------------+

,另一个是

mysql> describe accessories_cash_sales_items;
+--------------------------+------------------+------+-----+---------+----------------+
| Field                    | Type             | Null | Key | Default | Extra          |
+--------------------------+------------------+------+-----+---------+----------------+
| id                       | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| accessories_id           | int(10) unsigned | NO   | MUL | NULL    |                |
| quantity                 | int(11)          | NO   |     | 0       |                |
| amount                   | int(11)          | NO   |     | 0       |                |
| accessories_cash_sale_id | int(10) unsigned | NO   | MUL | NULL    |                |
| created_at               | timestamp        | YES  |     | NULL    |                |
| updated_at               | timestamp        | YES  |     | NULL    |                |
+--------------------------+------------------+------+-----+---------+----------------+

使用

调用刀片模板中的范围方法
\App\AccessoriesCashSale::paidOrCollected()

并且在现金销售模型中,paidOrCollected函数看起来像

public function scopePaidOrCollected($query){
    $amount = 0;
    $items = $query->where('status', '=', 'paid')->orWhere('status', '=', 'Collected')->whereMonth('created_at','=', date('m'))->get();

    foreach($items as $item){
        $amount = $amount + $item->accessoriesCashSalesItems()->sum('amount');;
    }

    return $amount;
}

1 个答案:

答案 0 :(得分:2)

使用范围时,应始终返回已修改的查询对象。

public function scopePaidOrCollected($query){
    return $query->where('status', '=', 'paid')->orWhere('status', '=', 'Collected')->whereMonth('created_at','=', date('m'));
}

使用您的范围过滤查询结果,然后将其传递到范围之外的其他方法,以迭代您的收藏并计算您的金额。

修改

您只需在模型中添加另一种处理范围数据的方法,如下所示:

public function getCashSaleAmounts()
{
    $items = AccessoriesCashSale::paidOrCollected()->get();
    $amount = 0;

    foreach($items as $item){
        $amount += $item->accessoriesCashSalesItems()->sum('amount');
    }

    return $amount;
}

但是,您可能需要在尝试循环之前检查结果。