Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在雄辩的关系中创建方法_Php_Laravel_Eloquent - Fatal编程技术网

Php 在雄辩的关系中创建方法

Php 在雄辩的关系中创建方法,php,laravel,eloquent,Php,Laravel,Eloquent,我有两个模型。一个是订单,另一个是项目。一个订单可以有多个项目,而一个项目只能有一个订单 我的订单模型中包含以下内容: public function items() { return $this->hasMany('App\Item', 'OrderNumber', 'OrderNumber'); } 这是我的商品模型: function order() { return $this->belongsTo('App\Order'); } 我想有一个函数,可以得到

我有两个模型。一个是订单,另一个是项目。一个订单可以有多个项目,而一个项目只能有一个订单

我的订单模型中包含以下内容:

public function items() {
    return $this->hasMany('App\Item', 'OrderNumber', 'OrderNumber');
}
这是我的商品模型:

function order() {
    return $this->belongsTo('App\Order');
}
我想有一个函数,可以得到一个订单的所有项目的总和。例如,我希望能够使用$order->items->totalPrice来检索总价

我尝试将此添加到项目模型中,但没有成功

public function totalPrice() {

    $total = 0;

    foreach($this as $item) {
        $total += $item->AmountPaid;
    }

    return $total;

}
下面是我得到的错误:

Undefined property: Illuminate\Database\Eloquent\Collection::$totalPrice

要实现我的目标,最好/最干净的方法是什么?更好的是,Laravel是否有一种内置的合计方法?

您可以尝试以下方法:

$order = Order::with('items')->find(10);
$total = $order->totalPrice();
在订单模型中,您需要编写:

public function totalPrice()
{
    $total = 0;

    if (!empty($this->items)) {
        foreach($this->items as $item) {
            $total += $item->AmountPaid;
        }
    }

    return $total;
}

这不是最优雅的方式,但可能值得研究

您可以尝试以下方法:

$order = Order::with('items')->find(10);
$total = $order->totalPrice();
在订单模型中,您需要编写:

public function totalPrice()
{
    $total = 0;

    if (!empty($this->items)) {
        foreach($this->items as $item) {
            $total += $item->AmountPaid;
        }
    }

    return $total;
}

这不是最优雅的方式,但可能值得研究

由于此关系的动态属性将返回一个集合,您可以从订单访问此集合。我们可以使用sum方法来帮助我们:

public function totalPrice()
{
    return $this->items->sum('AmountPaid');
}
然后在订单实例上调用它

$order->totalPrice();
如果您要检索大量订单记录并通过它们旋转以获得这些总计,那么您需要立即加载关系以避免N+1

$orders = Order::with('items')->....->get();

由于此关系的dynamic属性将返回一个集合,因此您可以从Order访问它。我们可以使用sum方法来帮助我们:

public function totalPrice()
{
    return $this->items->sum('AmountPaid');
}
然后在订单实例上调用它

$order->totalPrice();
如果您要检索大量订单记录并通过它们旋转以获得这些总计,那么您需要立即加载关系以避免N+1

$orders = Order::with('items')->....->get();

$order->items是一个集合,不是项目模型的实例。。。加载hasMany关系始终返回一个集合,该集合可能为空或包含许多相关模型$order->items是一个集合,而不是项目模型的实例。。。加载hasMany关系总是返回一个集合,该集合可能是空的,或者包含许多相关的模型,这与此解决方案无关。这是合计项目的正确方法,除非您想求助于进行SQL求和。SQL在大多数情况下都是SQL,因为DB可以完成这项工作,但是根据缓存设置的不同,它也可能非常快。这个解决方案没有什么不雅观之处。这是合计项目的正确方法,除非您想求助于进行SQL求和。SQL在大多数情况下都可以,因为DB可以完成这项工作,但是根据缓存设置的不同,这也可能非常快。啊,谢谢。返回$this->items->sum'AmountPaid';为我工作很出色,谢谢你。返回$this->items->sum'AmountPaid';对我来说效果很好