Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/226.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获得以db为单位的列平均值_Php_Mysql_Laravel - Fatal编程技术网

Php 如何从laravel获得以db为单位的列平均值

Php 如何从laravel获得以db为单位的列平均值,php,mysql,laravel,Php,Mysql,Laravel,我的事务表有4列(其他与查询无关) trans_id:整数,pk 用户标识:整数,fk 金额:双倍 总线id:整数 我试图通过对amount列求平均值来获得他们的平均消费金额,但我的回答一直是“找不到”列 public function userDollarAverage(Request $request) { $business = $request->input('bus_id'); $user = $request->input('user_id')

我的事务表有4列(其他与查询无关)

  • trans_id:整数,pk
  • 用户标识:整数,fk
  • 金额:双倍
  • 总线id:整数
我试图通过对
amount
列求平均值来获得他们的平均消费金额,但我的回答一直是“找不到”列

  public function userDollarAverage(Request $request)
  {
    $business = $request->input('bus_id');
    $user = $request->input('user_id');
    $getAvgAmount = Transactions::whereColumn([
      ['user_id', '=', $user],
      ['bus_id', '=', $business]
      ])->avg('amount');
    return response()->json(['average' => $getAvgAmount]);
  }
我的迁移

  Schema::create('Transactions', function (Blueprint $table) {
            $table->increments('transaction_id');
            $table->unsignedInteger('user_id');
            $table->foreign('user_id')->references('user_id')->on('users');
            $table->integer('bus_id')->nullable();
            $table->double('amount')->nullable();
        });
我路过

{
    "user_id": 51,
    "bus_id": 1
}
它们也都列在我的模型下
protected$filleble=[]

但我还是有错误:

  • Illumb\Database\QueryException:SQLSTATE[42S22]:未找到列:“where子句”中的1054未知列“51”(SQL:选择平均值(
    amount
    )作为从
    事务
    where(
    用户id
    =
    51
    总线id
    =
    1
    )的聚合)在第664行的文件C:\Users\Jermayne\API\vendor\laravel\framework\src\illumb\Database\Connection.php中
  • 我不明白为什么我会得到“51”专栏

whereColumn
更改为
where
,您应该已经上路了


whereColumn
用于比较各个列。

为什么要调用
whereColumn
?您没有将列相互比较。。。另外,
whereColumn
会寻找一个key=>value数组来比较column=>column我误解了
whereColumn()
的用法,我以为它是用来在
where()
中添加一个数组和使用多个列的,我需要使用
where()
而不是
whereColumn()