Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.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
使用一个不同的列Laravel获取查询_Laravel_Laravel 7 - Fatal编程技术网

使用一个不同的列Laravel获取查询

使用一个不同的列Laravel获取查询,laravel,laravel-7,Laravel,Laravel 7,在customer\u names中,我得到一个值数组,从那里我只想得到在值处创建的第一个。使用当前代码,我得到了多行,其中包含customer\u names中的所有值 $query = $this->customer->newQuery()->select( 'customers.id', 'customers.sage_id', 'customers.account_name', 'customer_names.name a

customer\u names
中,我得到一个值数组,从那里我只想得到在
值处创建的第一个
。使用当前代码,我得到了多行,其中包含
customer\u names
中的所有值

$query = $this->customer->newQuery()->select(
      'customers.id',
      'customers.sage_id',
      'customers.account_name',
      'customer_names.name as name',
      'customer_names.novation_date as novation_date',
    )->withCount('contracts');
$query->leftJoin('customer_names', 'customers.id', 'customer_names.customer_id')->groupBy('customers.sage_id');
我还尝试了另一种方法,但得到了一个空数组:

$query = $this->customer->newQuery()->with([
    'customerNames' => function ($query){
        $query->oldest()->first();
    }
])
“SQLSTATE[42803]:分组错误:7错误:列 “客户名称.更新日期”必须出现在GROUP BY子句或 在集合中使用

如果我将其添加到groupBy以及名称和ID,则不会出现错误,但结果是相同的


左连接出错,请尝试以下格式

$query=customers::newQuery()

$query->select('customers.id','customers.sage_id','customers.account_name','customer_name.name as name','customer_name.novation_date as novation_date',)->withCount('contracts')

$query->leftJoin('customer\u name','customer\u name.id','=',customers.customer\u id')->groupBy('customers.sage\u id')


如果需要distinct列,请在查询中添加distinct()方法

您必须在Laravel应用程序中关闭sctrict模式

转到
config/database.php

在MySQL集合下

strict => false
它将开始工作

以下是数据库设置

'mysql' => [
            'driver' => 'mysql',
            'url' => env('DATABASE_URL'),
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'prefix_indexes' => true,
            'strict' => false,
            'engine' => null,
            'options' => extension_loaded('pdo_mysql') ? array_filter([
                PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
            ]) : [],
        ],
如果您正在使用Postgres 然后,您必须将所有列添加到您在选择中提到的组中

$query = $this->customer->newQuery()->select(
      'customers.id',
      'customers.sage_id',
      'customers.account_name',
      'customer_names.name as name',
      'customer_names.novation_date as novation_date',
    )->withCount('contracts');
$query->leftJoin('customer_names', 'customers.id', 'customer_names.customer_id')->groupBy(['customers.sage_id', 'customers.account_name', 'name', 'novation_date']);

我使用的是PostgreSQL,但我没有。我尝试了这个,但得到了相同的错误。customers.id“必须出现在GROUP BY中,如果我添加了,则响应不正常。