0
我正在使用Laravel。我试图将用户模型和消息模型与许多关系联系起来。 当我访问用户消息时,它说功能没有定义。但是那个函数被定义了。 这是我得到的错误无法从多关系访问模型
Call to undefined method Illuminate\Database\Eloquent\Collection::messages()
用户模型。
public function docs(){
return $this->belongsToMany('Doc');
}
public function messages(){
return $this->belongsToMany('Message');
}
消息模型。
public function users(){
return $this->belongsToMany('User');
}
我想存储所选用户的消息。这是错误发生的地方。 我也设置了数据透视表。
消息控制器。
public function store()
{
//
$input = Input::only('title', 'body', 'sel_users');
$msg = Input::only('title', 'body');
$sel_users = Input::only('sel_users');
$this->messageForm->validate($input);
$insert = Message::create($msg);
$id = $insert['id'];
foreach ($sel_users as $userid) {
$user = User::find($userid);
$user->messages()->attach($id);
}
return Redirect::route('messages.index');
}
工作就像一个魅力。谢谢。 –