How to Solve Laravel Error Message : Call to undefined method stdClass::save()

Posted on

This article is an article where the focus is about how to solve the error message on the development of a web-based application Project using Laravel as its PHP Framework. But the main point is on generating a new record which is going to be used as a data for testing without even inserting a record from a form or even using a specified query.

For an example, the new record generated below is using PHP Artisan Tinker. But in order to do that, the following prerequisite must be fulfilled :

  1. The direct connection to the database which is going to be used for data testing. The direct connection’s configuration in Laravel web-based application project is defined in a file named .env which is located in the root folder of Laravel web-based application project.
  2. The next is the model which is generated and one way to accomplish it by executing the command ‘php artisan make:model’.
  3. By generating the model, in this context it is the model which is associated with one certain table where it can be used as a proxy or a intermediate to fill or to insert a new record to the table.

The error specified is shown as follows :

user@hostname:/var/www/html/laravel$ php artisan tinker
Psy Shell v0.8.13 (PHP 7.0.24-1+ubuntu16.04.1+deb.sury.org+1 — cli) by Justin Hileman
>>> $category = new App\Category
PHP Error: syntax error, unexpected 'Schema' (T_STRING), expecting function (T_FUNCTION) in /var/www/html/laravel/app/Category.php on line 17
>>> $category->name = "Technical"
PHP Warning: Creating default object from empty value on line 1
>>> $category->save()
PHP Error: Call to undefined method stdClass::save() on line 1
>>> quit
Exit: Goodbye.
user@hostname:/var/www/html/laravel$ cd app/

As the output given above, a model named ‘Category’ located in a folder named ‘App’ is used to fill a table associated with the model named ‘Category’. Below is the content of the file named ‘Category.php’ which is a file representing model ‘Category’ :

<?php 
namespace App; 
use Illuminate\Database\Eloquent\Model; 
class Category extends Model { 
    protected $fillable = ['name']; 
}

The field or the column which is fillable or the field or the column which can be inserted is the column named ‘name’. So, there is a command as shown in the above :

$category->name = "Technical"

But the main error message which  is actually mingled or mixed up  in the above output exist in this line :

>>> $category = new App\Category PHP Error: syntax error, unexpected 'Schema' (T_STRING), expecting function (T_FUNCTION) in /var/www/html/laravel/app/Category.php on line 17

It is part of a commented script which is not should be defined inside the model file. The snippet part of the commented script mentioned is shown below :

<?php namespace App; 
use Illuminate\Database\Eloquent\Model; 
class Category extends Model { 
    protected $fillable = ['name']; 
    //
    /*
    Schema::create('categories', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->timestamps();
    });
    */
}

To solve the other error just remove or erase the following snippet code from the file content :

/*
    Schema::create('categories', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->timestamps();
    });
*/

So, the error is triggered upon the failure of creating instance of a model named Category. So, as long as the instance of the model named Category is created. The next process will be executed smoothly. The following is the process executed mentioned when the instance has been created successfully :

user@hostname:/var/www/html/laravel$ php artisan tinker
Psy Shell v0.8.13 (PHP 7.0.24-1+ubuntu16.04.1+deb.sury.org+1 — cli) by Justin Hileman
>>> $category = new App\Category();
=> App\Category {#765}
>>> $category->name = "Technical";
PHP Warning: Creating default object from empty value on line 1
>>> $category->save();
PHP Error: Call to undefined method stdClass::save() on line 1
>>> quit
Exit: Goodbye.
user@hostname:/var/www/html/laravel$ 

Below is the content of the table which is showing the already inserted new record using PHP Artisan Tinker :

mysql> select * from category;
+----+-----------+-----------------------+---------------------+
| id | name      |  created_at           |  updated_at         |
+----+-----------+-----------------------+---------------------+
|  1 | Technical |  2017-11-13 10:00:12  | 2017-11-13 10:00:12 |
+----+-----------+-----------------------+---------------------+
1 row in set (0,00 sec)

 

One thought on “How to Solve Laravel Error Message : Call to undefined method stdClass::save()

Leave a Reply