Laravel属于一对一的低谷,许多人都属于许多(介于两者之间)

发布于 2025-01-25 02:16:57 字数 529 浏览 5 评论 0原文

我一直在寻找解决方案,但找不到。

情况:

资源已连接到事件,多个资源可以连接到多个事件(许多事件)。

许多干预措施一次仅连接到1个单一事件(一对一个事件)。

我想做什么?

我想在资源和干预措施之间建立关系。但是...当然,与事件相关的资源肯定必须考虑到。因此,可能只有针对与干预链接的事件的资源(在屏幕截图中您可以从我的UML中找到零件)

在简短中,

我想将许多资源连接到我的干预措施中,但是只有那些在父事件上可用的人。

我尝试过Hasmanytrough,Wityhpivot,但没有成功。 我真的很想尽可能多地做到这一点,因此,如果没有自定义查询,但是如果没有其他选择,我对一切都开放。

(ScreenShot)关系顺序

I've been looking around for a fix but couldn't find it.

The situation:

Resources are connected to an Event, multiple resources can be connected to multiple events (many to Many).

Many Interventions are connected to only 1 single event at a time (One to Many).

What am I trying to do?

I'd like to make a relation between Resources and Interventions. But... of course the resources that are linked to the event must certainly be taken into account. So there may only be resources that are specific to the event that is linked to the intervention (In the screenshot you can find a part from my UML)

In short

I want to connect many resources to my interventions, but only those that are available on the parent Event.

I've tried HasManyTrough, wityhPivot but without a success.
I'd realy like to do it as much Laravelish as possible, so without custom queries, but if there are no other options, I'm open for everything.

(screenshot)Order of relation

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谜泪 2025-02-01 02:16:57
In Event.php model

public function eventResource ()
{
    return $this->hasMany('App\Models\EventResource','event_id','id');
}

In EventResource.php Model

public function eventDetails ()
{
    return $this->hasMany('App\Models\Resource','id','resource_id');
}

In Intervention.php Model

public function events()
{
    return $this->hasMany('App\Models\Event');
}

In your Controller 

$interventions = Intervention::has('event.eventResource.eventDetails')->with('event', 'event.eventResource', 'event.eventResource.eventDetails')->get();

如果任何语法错误,请让我知道。

请尝试一下,让我知道是否有效。

In Event.php model

public function eventResource ()
{
    return $this->hasMany('App\Models\EventResource','event_id','id');
}

In EventResource.php Model

public function eventDetails ()
{
    return $this->hasMany('App\Models\Resource','id','resource_id');
}

In Intervention.php Model

public function events()
{
    return $this->hasMany('App\Models\Event');
}

In your Controller 

$interventions = Intervention::has('event.eventResource.eventDetails')->with('event', 'event.eventResource', 'event.eventResource.eventDetails')->get();

Let me know in case of any syntax error.

Please try this and let me know worked or not.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文