【问题标题】:Laravel Model Query Error : "Illuminate\Database\QueryException: SQLSTATE[23000]: Integrity constraint violation"Laravel 模型查询错误:“Illuminate\Database\QueryException:SQLSTATE[23000]:完整性约束违规”
【发布时间】:2019-11-17 11:37:29
【问题描述】:

我尝试加入 2 个表,但出现此错误:

Illuminate\Database\QueryException: SQLSTATE[23000]: 完整性约束违规: 1052 where 子句中的 'user_id' 列不明确(SQL: select sadaqah_history.total, sadaqah_history .foundation_donate_id 来自dzikir_counter 内部连接sadaqah_historysadaqah_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 行

我的 API 控制器代码:

$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();

模型 1 (Dzikir_Counter):

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'
];

模型 2 (Sadaqah_History):

    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 Query 与此相同:

选择 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 inner join 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

【问题讨论】:

    标签: php mysql laravel


    【解决方案1】:

    替换

    DzikirCounter::where('user_id', $user->id)

    DzikirCounter::where('dzikir_counter.user_id', $user->id)

    您得到的错误意味着user_id 列同时存在于dzikir_countersadaqah_history 表中。在这种情况下,您应该指定应该执行 WHERE 的表。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-02
      • 2020-01-31
      • 2021-03-22
      • 1970-01-01
      • 2017-09-11
      • 1970-01-01
      • 2021-01-26
      • 2015-07-17
      相关资源
      最近更新 更多