'Now laravel model instace not have any attribute

i have update my table in the database and add new column user_id but when i create new model instance i don't find this column as attribute, actually i don't have any attribute in the model instance

Model

class Event extends Model
{
    use HasFactory;
   
    public $timestamps = true;
    protected $fillable = ['reminder_id','title','start_at','end_at','all_day','description','folder_task_id','user_id'];

    // relations 
    public function folder_task()
    {
        return $this->belongsTo(FolderTask::class);
    }

    public function timer()
    {
        return $this->hasOne(Timer::class);
    }
}

Controller

$this->event = new Event();
dd($this->event);

Result

enter image description here



Solution 1:[1]

Laravel uses an active records pattern for the models. In this design pattern / architecture. The Model will not know it's columns, before they hit the database. Fillable is not a definition, more of a guard to ensure, you can't overwrite all columns from requests or similar.

In your example your Model, has not been saved in the database and therefor does not have any attributes. If you create the Model with data it will show.

$event = new Event(['title' => 'Your title','start_at' => now(),'end_at' => now()]);
$event->save();
dd($event); // attributes will be 'title', 'start_at' and 'end_at'

Or similar if you fetch the Model, it will also show the attributes, as it will fetch it will read the columns from the database.

dd(Event::find(1)); // attributes will be all columns from the database

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mrhn