【问题标题】:Call to a member function profile() on string error在字符串错误时调用成员函数 profile()
【发布时间】:2019-11-04 18:56:42
【问题描述】:

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

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

    return 'Success';
}

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

【问题讨论】:

  • 显然,当您调用函数 removeProfile() 时,您将传递一个字符串作为参数。你能添加调用函数的代码吗?

标签: string laravel function member


【解决方案1】:

在你的模型中平铺这个

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

在你的控制器中应该是

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

【讨论】:

    猜你喜欢
    • 2019-06-20
    • 2017-12-17
    • 2022-11-10
    • 2017-03-24
    • 2019-10-12
    • 2015-07-27
    • 2019-02-11
    相关资源
    最近更新 更多