【问题标题】:(E_ERROR) Too few arguments to function Symfony\Component\HttpKernel\Profiler\Profile::__construct(), 0 passed(E_ERROR) Symfony\Component\HttpKernel\Profiler\Profile::__construct() 函数的参数太少,通过了 0
【发布时间】:2019-08-18 08:35:21
【问题描述】:

我想使用属于和 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. 

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    要解决此问题,请将 User.php 文件顶部的 use Symfony\Component\HttpKernel\Profiler\Profile; 行替换为 use App\Profile;

    发生这种情况是因为您在 User.php 文件的顶部错误地包含了错误的类。当 Laravel 尝试加载关系时,它会尝试构造一个 Symfony\Component\HttpKernel\Profiler\Profile 对象,而不是构造您想要的模型。

    【讨论】:

      【解决方案2】:

      在您的用户模型中使用如下方式

      public function profile()
          {
              return $this->hasOne('App\Profile', 'foreign_key');
          }
      

      我不确定您为什么在用户模型中使用Symfony\Component\HttpKernel\Profiler\Profile。当您建立关系时,它使用的是 Profile 而不是您的个人资料模型。您必须在定义关系时使用 Profile Model 命名空间。

      【讨论】:

        猜你喜欢
        • 2021-02-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-02-22
        • 2021-05-28
        相关资源
        最近更新 更多