我正在尝试将消息附加到对话(多对多),但我收到了错误消息。我错过了什么?
数据透视表架构
conversation_message
(conversation_id
,message_id
)
模型
class Conversation extends Eloquent
{
public function messages()
{
$this->belongsToMany('Message', 'conversation_message', 'conversation_id', 'message_id');
}
}
class Message extends Eloquent
{
public function conversations()
{
$this->belongsToMany('Conversation', 'conversation_message', 'message_id', 'conversation_id');
}
}
控制器
$conversation = Conversation::find(1);
$message = Message::find(1);
$conversation->messages()->attach($message);
错误
Call to a member function attach() on a non-object
答案 0 :(得分:5)
两种方法都缺少return
个:
class Conversation extends Eloquent
{
public function messages()
{
return $this->belongsToMany('Message', 'conversation_message', 'conversation_id', 'message_id');
}
}
class Message extends Eloquent
{
public function conversations()
{
return $this->belongsToMany('Conversation', 'conversation_message', 'message_id', 'conversation_id');
}
}