Laravel Error Message : Access level to $timestamps must be public (as in class Illuminate\Database\Eloquent\Model)

Posted on

This is an article where an error message triggered. The article will discuss on how to solve the error message triggered. But it is obvious since the error message actually point out directly the error. It is an error where a web-based application powered by Laravel framework is being executed. The following is the scenario which is the one triggered the error message.

A model named Test was created by executing a command shown as follows :

user@hostname:/var/www/html/laravel-test$ php artisan make:model Test
Model created successfully.

Since the model represents a table in a web-based on Laravel framework development, there is a need to define a field named ‘$timestamps’. It is a field for defining whether or whether not the table will have a timestamps to mark the time of a record creation and modification. By default, without having to define the timestamps field, every record operation on creation and modification will also involving a field named ‘updated_at’ and ‘created_at’ which has a field type of datetime in this context on MySQL Database Server.

It is a must for an access level of a field named $timestamps which exist in a model named Test in the namespace of App or in other words it is located in a folder named App to have an access modifier of public.

Access level to App\Test::$timestamps must be public (as in class Illuminate\Database\Eloquent\Model)

Below is the original declaration of $timestamps field in the file named Test.php :

class Test extends Model {
      protected $timestamps = false;

The declaration of a field named ‘$timestamps’ above is set with a value of ‘false’. It is done so that every creation or modification of a record in the table associated with the model class file named ‘Test’ will ignore or will not attempt to fill or to add a data into the field or column named ‘updated_at’ and ‘created_at’.

So, the problem is the access modifier which is stated in the Laravel error message. It cannot be declared with a ‘protected’ access modifier. So, to solve it, try to change it into a ‘public’ access modifier as shown below :

public $timestamps = false;

Overall, the model class definition will be shown below :

class Test extends Model {
      public $timestamps = false;

One thought on “Laravel Error Message : Access level to $timestamps must be public (as in class Illuminate\Database\Eloquent\Model)

Leave a Reply