Integrity constraint violation: 1052 Column 'updated_at' in field list is ambiguous

Viewed 5456

Content of App\User Model file:-

<?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 migration file:-

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

in the user->message() method when i put the column "updated_at" or "created_at" on the ->select method i got error :-

"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))"

when i totally remove the ->select() method from the query builder, i got response with all columns including the created_at and updated_at column.

How can i fix this?

2 Answers

That's because your KhatmaRead and Khatma models has update_at column and here:

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

You don't specify from what table you will take that column and it's ambiguous. You should do something like this:

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

if there are any same name column between tables must put name table before column

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