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

Posted on

Another article discussing on how to solve the error experienced when developing a web-based application using Laravel as its framework development. This is an error arises on executing a script from a certain controller file. A controller within its content, it has a several line of codes performing an operation on a model.

The operation is actually calling a method named ‘save()’. This is a method derived from its parent class, the Controller class. It is a default method inherited directly if a controller class created. The creation of the controller class is done by executing the following command with the help of artisan tool provided in the project which is installed with a Laravel framework as shown below :

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

After successfully created a controller. Within the controller itself, there is a method executed with a line of code calling the save method on the model as shown below :

Test::save()

But apparently, on this method, it is not working as an error shown and it is triggered as follows :

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

This error relates on the attribute defined in a file which is specifically a model file named Test. The following snippet code is pointing out the possible error which might be the cause of the problem :

class Test extends Model
{
...
protected $incrementing = true;
...
}

As it is stated clearly in the error message given above, the problem lies in the access modified which is used to define the attribute of the model class named Test. The attribute named $incrementing which is used to specify the behavior of ‘auto_increment’ mechanism which is owned by a default primary key id in a table must be defined as a public access modifier instead of protected. So, to be able to solve the problem above, just change the access modifier of the attribute variable named $incrementing into public as shown below :

class Test extends Model
{
...
public $incrementing = true;
...
}

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

Leave a Reply