【问题标题】:PHP Error: Call to undefined method stdClass::save() in Psy Shell code on line 1PHP 错误:在第 1 行的 Psy Shell 代码中调用未定义的方法 stdClass::save()
【发布时间】:2020-04-22 07:41:18
【问题描述】:

我是 Laravel 的新手, 我正在使用 tinker 创建记录:

$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 Error: Call to undefined method stdClass::save() in Psy Shell code on line 1

这里是我的 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();
        });
    }

提前致谢

【问题讨论】:

    标签: laravel phpstorm laravel-artisan laravel-6 tinker


    【解决方案1】:

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

    $Profile=new \App\Profile(); 
    

    后来你用小写字母

    $profile->title='Premier Titre'
    

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

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

    表示创建了新对象

    所以一定要改变

    $Profile=new \App\Profile(); 
    

    进入

    $profile=new \App\Profile(); 
    

    让它工作

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-24
      • 1970-01-01
      • 1970-01-01
      • 2012-04-02
      • 2018-12-16
      • 1970-01-01
      • 1970-01-01
      • 2018-10-16
      相关资源
      最近更新 更多