![](/img/trans.png)
[英]Illuminate\Database\QueryException SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'cnic' cannot be null
[英]Laravel Model Query Error : "Illuminate\Database\QueryException: SQLSTATE[23000]: Integrity constraint violation"
我嘗試加入 2 個表,但收到此錯誤:
照射\\數據庫\\ QueryException:SQLSTATE [23000]:完整性約束違規:1052列在where子句是不明確的(SQL 'USER_ID':選擇
sadaqah_history
。total
,sadaqah_history
。foundation_donate_id
從dzikir_counter
內部聯接sadaqah_history
上sadaqah_history
。user_id
=dzikir_counter
。user_id
其中user_id
= 25 和 DATE_FORMAT(dzikir_date, '%Y-%m-%d') BETWEEN '2019-07-01' AND '2019-07-31') 在文件 /var/www/backend/z/vendor/laravel /framework/src/Illuminate/Database/Connection.php 第 664 行
$startDate = Carbon::parse(new Carbon('first day of this month'))->toDateString();
$endDate = Carbon::parse(new Carbon('last day of this month'))->toDateString();
$models = DzikirCounter::where('user_id', $user->id)
->whereRaw("DATE_FORMAT(dzikir_date, '%Y-%m-%d') BETWEEN '$startDate' AND '$endDate'")
->join('sadaqah_history', 'sadaqah_history.user_id', '=', 'dzikir_counter.user_id')
->get(['sadaqah_history.total', 'sadaqah_history.foundation_donate_id'])
->actived()
->orderBy('dzikir_date', 'desc')
->get();
protected $table = 'dzikir_counter';
protected $fillable = [
'user_id',
'counter',
'dzikir_total',
'dzikir_date',
'total',
'dzikir_detail',
'status',
'deleted_at',
'created_at',
'updated_at',
'created_by',
'updated_by',
];
protected $appends = [
'dzikir_details'
];
protected $hidden = [
'dzikir_detail',
'user_id',
'created_by',
'updated_by',
'deleted_at'
];
protected $casts = [
'counter' => 'int',
'dzikir_total' => 'int',
'status' => 'int',
'user_id' => 'int'
];
protected $table = 'sadaqah_history';
protected $fillable = [
'user_id',
'name',
'point_total',
'total',
'sadaqah_date',
'status',
'is_dzikir',
'foundation_donate_id',
'created_at',
'updated_at',
'created_by',
'updated_by',
];
protected $hidden = [
'id',
'user_id',
'name',
'point_total',
'created_at',
'updated_at',
'created_by',
'updated_by',
'foundation_donate_id',
'created_by',
'updated_by'
];
protected $casts = [
'status' => 'int',
'is_dzikir' => 'int',
'point_total' => 'int',
'total' => 'int',
'user_id' => 'int',
'foundation_donate_id' => 'int',
];
我想要實現的是與此相同的SQL 查詢:
SELECT dzikir_counter.user_id,dzikir_counter.dzikir_date,sadaqah_history.sadaqah_date,dzikir_counter.dzikir_total,sadaqah_history.point_total,sadaqah_history.total,sadaqah_history.foundation_donate_id FROM
dzikir_counter
內加入sadaqah_history
ON dzikir_counter.user_id = sadaqah_history.user_id AND dzikir_counter.dzikir_date = sadaqah_history.sadaqah_date
按sadaqah_history
。foundation_donate_id
DESC
代替
DzikirCounter::where('user_id', $user->id)
和
DzikirCounter::where('dzikir_counter.user_id', $user->id)
您得到的錯誤意味着user_id
列同時存在於dzikir_counter
和sadaqah_history
表中。 在這種情況下,您應該指定 WHERE 應該執行哪個表。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.