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 每次我想访问Laravel中我的模型中的hasMany集合时,都会出现ERR_CONNECTION_RESET_Php_Laravel_Vue.js - Fatal编程技术网

Php 每次我想访问Laravel中我的模型中的hasMany集合时,都会出现ERR_CONNECTION_RESET

Php 每次我想访问Laravel中我的模型中的hasMany集合时,都会出现ERR_CONNECTION_RESET,php,laravel,vue.js,Php,Laravel,Vue.js,我有一个奇怪的问题,我无法处理。我在Laravel和Vue/Vuex中获得了一个用于前端的简单商店应用程序 我的订单模型与有许多关系: public function items() { return $this->hasMany(OrderItem::class); } 很好 我正在向vuex action发出GET/orders/请求,以从Laravel获取订单。这是我的OrderController@index操作: // Return JSON response if w

我有一个奇怪的问题,我无法处理。我在Laravel和Vue/Vuex中获得了一个用于前端的简单商店应用程序

我的
订单
模型与
有许多关系:

public function items()
{
    return $this->hasMany(OrderItem::class);
}
很好

我正在向vuex action发出
GET/orders/
请求,以从Laravel获取
订单。这是我的
OrderController@index
操作:

// Return JSON response if wantsJson otherwise just show View.
if ($this->request->wantsJson()) {
    $orders = Order::with(['client', 'items'])->orderBy('id', 'desc')->get();

    return response()->json($orders);
}

return view('panel.orders.index');
正如我们在下面看到的,它作为异常返回的JSON响应使用
orders

每个
订单
都有两个
$附录
字段:
total\u net
total\u net\u折扣后
及其方法:
getTotalNetAttribute()
getTotalNetAfterDiscountsAttribute()

对于
$appends
计算,我需要一个
项目
关系项目。因此,在
getTotalNetAttribute()方法中,我试图获取一个关系项:

public function getTotalNetAttribute()
{
    $items = $this->items;
}
当我将
$this->items
添加到方法中时,我得到了一个响应:ERR\u CONNECTION\u RESET。在那之后,我的整个反应都被打破了。我甚至不能记录它:

public function getTotalNetAttribute()
{
    logger($this->items); // it just breaking up my response: ERR_CONNECTION_RESET
}

另一件奇怪的事情是它在
tinker
中工作。我的意思是,我可以使用
$this->items
,对它们的价格求和,并毫无问题地获得
$order->total_net

编辑: 我找到了一个解决办法:

public function getNetPriceAttribute()
{
    $orderItems = $this->hasMany(OrderItem::class)->get();

    return $orderItems->sum(function ($item) {
        return $item->net_price;
    });
}

那么
return$this->items()->sum('net_price')呢?不幸的是,“净价”不是一列,它是一个$append字段。在您的解决方法
getNetPriceAttribute()
return$item->net\u price是否未引用items表上的db列?那你的蛇壳就要把我甩了。那
return$this->items()->sum('net_price')呢?不幸的是,“净价”不是一列,它是一个$append字段。在您的解决方法
getNetPriceAttribute()
return$item->net\u price是否未引用items表上的db列?那你的蛇箱就要把我甩了。