gpt4 book ai didi

laravel - 完整性约束违规 : 1052 Column 'updated_at' in field list is ambiguous

转载 作者:行者123 更新时间:2023-12-05 00:56:18 33 4
gpt4 key购买 nike

App\User Model 文件内容:-

<?php

namespace App;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Tymon\JWTAuth\Contracts\JWTSubject;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable implements JWTSubject
{
use Notifiable;

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
];

public function khatmas()
{
return $this->hasMany('App\Khatma');

}

public function messages()
{
return $this->hasManyThrough('App\KhatmaRead','App\Khatma')
->select('reader_name','message','updated_at')
->where('message','!=',NULL);
}

/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];

/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];

public function getJWTIdentifier()
{
return $this->getKey();
}

/**
* Return a key value array, containing any custom claims to be added to the JWT.
*
* @return array
*/
public function getJWTCustomClaims()
{
return [];
}
}

khatma_reads 迁移文件:-

    $table->bigIncrements('id');
$table->unsignedBigInteger('khatma_id');
$table->foreign('khatma_id')->references('id')->on('khatmas')->onDelete('cascade');
$table->string('reader_name')->nullable();
$table->longText('message')->nullable();
$table->timestamps();

在 user->message() 方法中,当我将列“updated_at”或“created_at”放在 ->select 方法上时出现错误:-

"SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'updated_at' in field list is ambiguous (SQL: select `reader_name`, `message`, `updated_at`, `khatmas`.`user_id` as `laravel_through_key` from `khatma_reads` inner join `khatmas` on `khatmas`.`id` = `khatma_reads`.`khatma_id` where `message` is not null and `khatmas`.`user_id` in (1))"

当我从查询构建器中完全删除 ->select() 方法时,我得到了所有列的响应,包括 created_at 和 updated_at 列。

我该如何解决这个问题?

最佳答案

那是因为你的 KhatmaReadKhatma 模型有 update_at 列,这里:

->select('reader_name','message','updated_at')

您没有指定要从哪个表中获取该列,并且它是模棱两可的。你应该这样做:

->select('reader_name','message','khatma_reads.updated_at')

关于laravel - 完整性约束违规 : 1052 Column 'updated_at' in field list is ambiguous,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62505572/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com