我想使用属于和 hasOne 的关系将配置文件模型与现有用户模型相关联,但我收到了该错误。这是我的 Profile.php <?phpnamespace App;use Illuminate\Database\Eloquent\Model;class Profile extends Model{ public function user(){ return $this->belongsTo(User::class); }}用户名.php <?phpnamespace 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->profileTypeError: 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.
2 回答
杨魅力
TA贡献1811条经验 获得超6个赞
要解决此问题,请将User.php文件use Symfony\Component\HttpKernel\Profiler\Profile;
顶部的行替换为。use App\Profile;
这是因为您错误地在User.php
文件顶部包含了错误的类。当 Laravel 尝试加载关系时,它会尝试构建一个Symfony\Component\HttpKernel\Profiler\Profile
对象,而不是构建您想要的模型。
千巷猫影
TA贡献1829条经验 获得超7个赞
在您的用户模型中使用如下所示
public function profile()
{
return $this->hasOne('App\Profile', 'foreign_key');
}
我不确定你为什么Symfony\Component\HttpKernel\Profiler\Profile在你的用户模型中使用。当您建立关系时,它使用的是它Profile而不是您的个人资料模型。您必须在定义关系时使用 Profile Model 命名空间。
- 2 回答
- 0 关注
- 232 浏览
添加回答
举报
0/150
提交
取消