我有一个代码:
$orders = Order::all();
$allorders = $orders->count();
$deliveryQuery = $orders->where('status', '=', '8')->select(DB::raw('AVG(updated_at) as order_average'))->first();
我收到错误:
Method select does not exist.
我该如何解决?如果我这样做,我的代码正在运行:
$deliveryQuery = Order::where('status', '=', '8')->select(DB::raw('AVG(updated_at) as order_average'))->first();
但这不好,我想要1个查询,但不是2 ..
更新
$orders->where('status', '=', '8')->avg('updated_at')->first();
我可以使用这段代码吗?但它不起作用。获取错误:
Object of class Illuminate\Support\Carbon could not be converted to int
答案 0 :(得分:1)
all()方法返回Collection。然后,您可以对结果使用Collection methods,但where()和select()是QueryBuilder类的方法。
$query = Order::query(); // You can get the query builder this way.
$orders = Order::all(); // equivalent to $query->get(); => return a Collection
$orderCount = Order::count(); // equivalent to $query->count();
$orderCount = $orders->count(); // here $orders is a Collection and the count() method is from the Collection class
当您调用avg()时,您将从Collection类中调用它。但它只能对数字起作用,而且Laravel会将updated_at属性解析为Carbon日期。
您的代码可能是:
$query = Order::query();
$orders = $query->get();
$allorders = $query->count();
$deliveryQuery = $query->where('status', '=', '8')->select(DB::raw('AVG(updated_at) as order_average'))->first();
答案 1 :(得分:0)
你可以试试这个..
const https = require('https');
const request = require('request');
request({
'url':'https://teamtreehouse.com/chalkers.json',
'proxy':'http://xx.xxx.xxx.xx'
},
function (error, response, body) {
if (!error && response.statusCode == 200) {
var data = body;
console.log(data);
}
}
);
可以找到类似的问题here