PHP 错误:在第 1 行的 Psy Shell 代码中调用未定义的方法 stdClass::save()



我是拉拉维尔的新手, 我正在使用修补匠来创建记录:

$Profile=new AppProfile();
=> AppProfile {#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((

这是我的个人资料.php

<?php
namespace App;
use IlluminateDatabaseEloquentModel;
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();
});
}

提前致谢

这里的问题是你用大写字母定义变量:

$Profile=new AppProfile(); 

后来你用小写字母使用它

$profile->title='Premier Titre'

因此,可能在幕后创建了新的 stdClass 对象,显然它没有save方法。您还会收到有关此的警告:

PHP 警告:从第 1 行的 Psy Shell 代码中的空值创建默认对象

表示创建新对象

所以一定要改变

$Profile=new AppProfile(); 

$profile=new AppProfile(); 

使其工作

最新更新