I am encountering an issue on Laravel, an exception is thrown because of an "undefined" created_at column on one of the database tables (but not the pivot table). The accessor in question is returning a human friendly format for the date. The data is sent to the client as JSON. Here are the tables:
class Link extends Model
{
use HasFactory;
protected $primaryKey='id';
protected $table='links';
protected $perPage=24;
protected $appends = [
'human_friendly',
'slug',
];
protected $fillable = [
'url',
'active',
'slug',
'title',
'site_name',
'twitter_site',
'description',
'locale'
];
protected $hidden = [];
protected $casts = [
'active' => 'boolean',
];
public function getSlugAttribute()
{
return Str::slug($this->attributes['slug']);
}
public function getHumanFriendlyAttribute()
{
// problem arises from here
return Carbon::parse($this->attributes['created_at'])->diffForHumans();
}
public function users()
{
return $this->belongsToMany(User::class);
}
}
class User extends Authenticatable implements JWTSubject
{
use HasApiTokens, HasFactory, Notifiable;
protected $primaryKey='id';
protected $table='users';
protected $perPage=24;
protected $appends = [];
protected $fillable = [
'username',
'active',
'email',
'password',
'slug',
];
protected $hidden = [
'password',
'remember_token',
];
protected $casts = [
'email_verified_at' => 'datetime',
'active'=>'boolean',
];
public function getJWTIdentifier()
{
return $this->getKey();
}
public function getJWTCustomClaims()
{
return [];
}
public function links()
{
return $this->belongsToMany(Link::class);
}
}
The pivot table also has timestamp columns (the migration schema):
Schema::create('link_user', function (Blueprint $table) {
$table->bigInteger('link_id')->unsigned()->index();
$table->bigInteger('user_id')->unsigned()->index();
$table->primary(['link_id', 'user_id']);
$table->timestamp('created_at')->useCurrent();
$table->timestamp('updated_at')->useCurrent();
$table->foreign('link_id')->references('id')->on('links')->onDelete('cascade');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
As you can see there is nothing out of the ordinary although I do suspect it's because the pivot table also have the timestamp columns. Has anyone else ran into this problem and shed some light on this problem?
Aside from this, the JSON is received very well. Thanks in advance.
The issue you're encountering is related to the
created_atcolumn not being available on thelinkstable, but it's being accessed in thegetHumanFriendlyAttributeaccessor of yourLinkmodel. The problem doesn't seem to be directly related to the pivot table's timestamps.To resolve this issue, you have a few options:
created_atcolumn exists in yourlinkstable. If it's not present and you want to use it in your accessor, you should create it in the table schema.If you don't intend to use these columns, you can omit the
$table->timestamps()line, and Laravel won't expect them.created_atcolumn to yourlinkstable, you can modify thegetHumanFriendlyAttributeaccessor to handle cases where the column might not exist. You can check if the column exists in the$attributesarray before using it:This way, if the
created_atcolumn is missing, it will return a default value or handle it as you see fit.created_atattribute in your JSON response, you can remove thegetHumanFriendlyAttributeaccessor altogether if it's not essential for your application.Choose the option that best fits your requirements, whether it's adding the
created_atcolumn, customizing the accessor, or removing it based on your specific needs.