duanquyong8164
2017-12-06 06:11
浏览 65
已采纳

在Laravel中调用未定义的方法错误

I have the following function:

public function addSubject(Request $request) {
    $student=User::find($request->invisible);
    $classsubjects = $request['classsubjects'];
    $student->classSubjects()->attach($cclasssubjects);
}

This gives me an error saying:

BadMethodCallException Call to undefined method Illuminate\Database\Query\Builder::attach()

图片转代码服务由CSDN问答提供 功能建议

我有以下功能:

  public function addSubject( 请求$ request){
 $ student = User :: find($ request-> invisible); 
 $ classsubjects = $ request ['classsubjects']; 
 $ student-> classSubjects() - > attach  ($ cclasssubjects); 
} 
   
 
 

这给我一个错误说:

BadMethodCallException \ n调用未定义的方法Illuminate \ Database \ Query \ Builder :: attach()

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • drqja5919276 2017-12-06 06:21
    已采纳

    The function classSubjects() in your Package model has to return a BelongsToMany relationship in order to use attach().

    public function classSubjects() {
      return $this->belongsToMany('classSubjects');
    }
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题