PHP错误:在第1行的Psy Shell代码中调用未定义的方法stdClass :: save() [英] PHP Error: Call to undefined method stdClass::save() in Psy Shell code on line 1
问题描述
我是Laravel新手,我正在使用修补匠来创建记录:
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
我遇到以下错误:PHP错误:在第1行的Psy Shell代码中调用未定义的方法stdClass :: save()
I have the following error:PHP Error: Call to undefined method stdClass::save() in Psy Shell code on line 1
这是我的profile.php
Here my profile.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Profile extends Model
{
public function user()
{
return $this->belongsTo(User::class );
}
}
这是我的迁移代码:
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();
});
}
预先感谢
推荐答案
此处的问题是您使用大写字母定义变量:
The problem here is that you define variable with capital letter:
$Profile=new \App\Profile();
然后再用小写字母
$profile->title='Premier Titre'
因此可能是在后台创建了新的stdClass对象,并且显然没有 save
方法.您还会收到关于此的警告:
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警告:从第1行的Psy Shell代码中的空值创建默认对象
PHP Warning: Creating default object from empty value in Psy Shell code on line 1
表示创建了新对象
因此请确保您进行更改
$Profile=new \App\Profile();
进入
$profile=new \App\Profile();
使其正常工作
这篇关于PHP错误:在第1行的Psy Shell代码中调用未定义的方法stdClass :: save()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!