作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
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 列。
我该如何解决这个问题?
最佳答案
那是因为你的 KhatmaRead
和 Khatma
模型有 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/
我是一名优秀的程序员,十分优秀!