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

こ雲淡風輕ζ 提交于 2021-01-05 12:56:53

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/59583700/php-error-call-to-undefined-method-stdclasssave-in-psy-shell-code-on-line-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!