laravel范围模型功能错误

laravel scope model function error

我正在尝试使用 laravel 中的范围对属于特定 cash_sale 的所有 cash_sales_items 求和。问题是当我在那些 table 中有一些信息时代码工作正常但是当 table 为空时我得到一个错误

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

下面是代码和table结构

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

正在使用

在 blade 模板中调用范围方法
\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;
}

使用范围时,您应该始终return 修改后的查询对象。

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

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