gpt4 book ai didi

php - Eloquent Attach() 处理非对象错误

转载 作者:行者123 更新时间:2023-12-02 09:47:29 26 4
gpt4 key购买 nike

我正在尝试将消息附加到对话(多对多),但出现错误。我错过了什么?

数据透视表架构

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');
}
}

Controller

$conversation = Conversation::find(1);
$message = Message::find(1);
$conversation->messages()->attach($message);

错误

Call to a member function attach() on a non-object

最佳答案

两种方法都缺少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');
}
}

关于php - Eloquent Attach() 处理非对象错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24742918/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com