Php Laravel中的多对多多态关系问题
所以我一直在做一个项目,有两种类型的用户注册。 1.单位 2.经销商 我已经为两者创建了模块,它们都使用passport创建了单独的身份验证模块 所有工作都很好,现在我想添加一个功能,允许从公司向分销商发送消息,反之亦然 以下是我的迁移: 1。信息 身份证 信息 父\u消息\u id(用于处理消息线程) 2。fromables(处理从哪个用户发送消息的多态关系) 身份证 消息id fromable_id fromable_类型('App\Company'或'App\Distributor') 3。toables(处理消息发送给哪个用户的多态关系) 身份证 消息id 烤面包 toable_类型(“应用程序\公司”或“应用程序\分销商”) 以下是如何在模型中定义关系:Php Laravel中的多对多多态关系问题,php,laravel,eloquent,Php,Laravel,Eloquent,所以我一直在做一个项目,有两种类型的用户注册。 1.单位 2.经销商 我已经为两者创建了模块,它们都使用passport创建了单独的身份验证模块 所有工作都很好,现在我想添加一个功能,允许从公司向分销商发送消息,反之亦然 以下是我的迁移: 1。信息 身份证 信息 父\u消息\u id(用于处理消息线程) 2。fromables(处理从哪个用户发送消息的多态关系) 身份证 消息id fromable_id fromable_类型('App\Company'或'App\Distributor') 3
$user = $request->user();
$messages_received = $user->messages_received;
$messages_sent = $user->messages_sent;
一直到这里都很好用
现在的问题是,当我运行下面的代码来接收所有消息时
$messages\u received=$user->messages\u received代码>
它返回所有消息,但不返回发送消息的人。接收到的资源消息中使用了“toable”关系。并且发送者ID处于'fromable'关系下
在这里,我不确定如何在完成时获取发件人详细信息以及所有邮件$user->messages\u received
我该怎么做?
如果有任何不同的方法来实现这个消息传递系统,也请告诉我
谢谢。据我所知,您应该能够执行$user->messages\u received->load('From\u company')
以获取消息发送到哪个公司的关系。据我所知,您应该能够执行$user->messages\u received->load('to\u company'))
要获取消息发送到哪个公司的关系?哇,你真是个传奇人物。这不是给你的公司,而是来自你的公司。我只是试了一下,效果不错。你能把它添加到答案中吗?我会把它标记为正确答案:)$user->messages\u received->load('from\u company');
public function from_company()
{
return $this->morphedByMany('App\Company', 'fromable');
}
public function from_distributor()
{
return $this->morphedByMany('App\Distributor', 'fromable');
}
public function to_company()
{
return $this->morphedByMany('App\Company', 'toable');
}
public function to_distributor()
{
return $this->morphedByMany('App\Distributor', 'toable');
}
$user = $request->user();
$messages_received = $user->messages_received;
$messages_sent = $user->messages_sent;