首页 > 解决方案 > 在字符串错误时调用成员函数 profile()

问题描述

我正在尝试从数据透视表中分离一个字段,但我一直收到错误“调用字符串上的成员函数”作为错误。我正在使用最新版本的 Laravel。

public function removeProfile($child)
{
    $profile = Profile::find(1);
    $child->profiles()->detach($profile);

    return 'Success';
}

Symfony \ Component \ Debug \ Exception \ FatalThrowableError (E_ERROR) 调用字符串上的成员函数profile()

标签: stringlaravelfunctionmember

解决方案


在你的模型中,一些东西平铺了这个

return $this->belongsToMany('profie')->withPivot('user', 'profile');

并且在您的控制器中应该是

$variable->profile()->where(some condition)->first()->pivot->your_entity

推荐阅读