Search code examples
laravelphpstormlaravel-artisanlaravel-6tinker

PHP Error: Call to undefined method stdClass::save() in Psy Shell code on line 1


I'm new in Laravel, I'm using tinker to create a record:

$Profile=new \App\Profile();
=> App\Profile {#3038}
>>> $profile->title='Premier Titre'
PHP Warning:  Creating default object from empty value in Psy Shell code on line 1
>>> $profile->title='Premier Titre';
=> "Premier Titre"
>>> $profile->description='Description';
=> "Description"
>>> $profile->user_id=1;
=> 1
>>> $profile->save()
PH

I have the following error:PHP Error: Call to undefined method stdClass::save() in Psy Shell code on line 1

Here my profile.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Profile extends Model
{
    public function user()

{
    return $this->belongsTo(User::class );
}
}

here is my migration code:

public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->string('surname')->unique();
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

thanks in advance


Solution

  • The problem here is that you define variable with capital letter:

    $Profile=new \App\Profile(); 
    

    and later you use it with small letter

    $profile->title='Premier Titre'
    

    So probably under the hood new stdClass object is created and obviously it doesn't have save method. You are also getting warning about this:

    PHP Warning: Creating default object from empty value in Psy Shell code on line 1

    which says new object is created

    So make sure you change

    $Profile=new \App\Profile(); 
    

    into

    $profile=new \App\Profile(); 
    

    to make it working