(E_ERROR) 函数 Symfony\Component\HttpKernel\

我想使用属于和 hasOne 的关系将配置文件模型与现有用户模型相关联,但我收到了该错误。


这是我的 Profile.php


    <?php


namespace App;


use Illuminate\Database\Eloquent\Model;


class Profile extends Model

{

    public function user(){

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

    }

}

用户名.php


     <?php


namespace App;


use Illuminate\Notifications\Notifiable;

use Illuminate\Contracts\Auth\MustVerifyEmail;

use Illuminate\Foundation\Auth\User as Authenticatable;

use Symfony\Component\HttpKernel\Profiler\Profile;


class User extends Authenticatable

{

    use Notifiable;


    /**

     * The attributes that are mass assignable.

     *

     * @var array

     */

    protected $fillable = [

        'name', 'email', 'username', 'password',

    ];


    /**

     * The attributes that should be hidden for arrays.

     *

     * @var array

     */

    protected $hidden = [

        'password', 'remember_token',

    ];


    /**

     * The attributes that should be cast to native types.

     *

     * @var array

     */

    protected $casts = [

        'email_verified_at' => 'datetime',

    ];


    public function profile()

    {

        return $this->hasOne(Profile::class);

    }

}

在我的终端中,我可以通过配置文件获取用户,但无法使用用户获取配置文件。这是错误


$user->profile

TypeError: Too few arguments to function Symfony/Component/HttpKernel/Profiler/Profile::__construct(), 0 passed in /Users/macair13/freeCodeGram/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php on line 720 and exactly 1 expected. 



慕沐林林
浏览 233回答 2
2回答

杨魅力

要解决此问题,请将User.php文件use Symfony\Component\HttpKernel\Profiler\Profile;顶部的行替换为。use App\Profile;这是因为您错误地在User.php文件顶部包含了错误的类。当 Laravel 尝试加载关系时,它会尝试构建一个Symfony\Component\HttpKernel\Profiler\Profile对象,而不是构建您想要的模型。

千巷猫影

在您的用户模型中使用如下所示public function profile()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return $this->hasOne('App\Profile', 'foreign_key');&nbsp; &nbsp; }我不确定你为什么Symfony\Component\HttpKernel\Profiler\Profile在你的用户模型中使用。当您建立关系时,它使用的是它Profile而不是您的个人资料模型。您必须在定义关系时使用 Profile Model 命名空间。
打开App,查看更多内容
随时随地看视频慕课网APP