1

I am trying to sum amount up all cash_sales_items that belong to particular cash_sale using scope in laravel. The problem is when i have some information in those tables the code works fine but when the tables are empty i get an error

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

below is the code and table structures

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    |                |
+---------------+------------------+------+-----+---------+----------------+

and the other is

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    |                |
+--------------------------+------------------+------+-----+---------+----------------+

Am calling the scope method in the blade template with

\App\AccessoriesCashSale::paidOrCollected()

and in the cash sale model paidOrCollected function looks like

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;
}
4

1 回答 1

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;
}

但是,您可能希望在尝试遍历它们之前检查您的结果。

于 2016-03-29T16:44:35.843 回答