使用嵌套关系时,如何返回单个模型? [英] How do I return a single Model when working with nested relationships?

查看:63
本文介绍了使用嵌套关系时,如何返回单个模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用MySQL数据库(v5.7)进行Laravel项目(v6.5.1),该数据库主要用于组织足球比赛.由于现在变得有点复杂,我在查询所有模型时都遇到了麻烦.让我进一步解释,这样更有意义:

I'm currently working on a Laravel Project (v6.5.1) with a MySQL DB (v5.7) which is primarily for organising football matches. I'm a bit stuck on querying all of my models as it is getting a bit complicated now. Let me explain further so it makes a bit more sense:

结构如下:

每个俱乐部都是独立的.

Each club is independant.

俱乐部表(ID,名称等).

Club table (id, name, etc).

每个俱乐部都有很多赛季,现在让我们从第1赛季开始. 季节表(编号,赛季编号,club_id) hasMany与匹配表的关系

Each club has many seasons, lets start with season 1 for now. Seasons Table (id, season number, club_id) hasMany relationship to Match table

每个赛季将进行许多比赛. 匹配表格(编号,季节编号和其他一些列) 与团队有很多关系

Each season will consist of many matches. Match Table (id, season id, some other columns) hasMany relationship with Team

每场比赛都会有一群报名参加比赛的玩家. 团队表(id,match_id,user_id和其他一些列)

Each Match will have a bunch of players who sign up to play. Team Table (id, match_id, user_id, some other columns)

我需要做的第一件事是,返回俱乐部目前所在的赛季.我通过以下雄辩的查询来做到这一点:

First thing I need to do is, return the current season that the Club is on. I do this with the following eloquent query:

我转到俱乐部表,使用$ url(从uri传递)查询它,返回匹配的第一个俱乐部,使用Club和Season之间的hasMany关系并返回最新的季节(因为它将始终是当前的)

I go to the Club Table, query it using the $url (which is passed from the uri), return the first club that matches, use the hasMany relationship between Club and Season and return the newest season (as that will always be the current)

Club::where('url_extension', $url)->first()->seasons->sortByDesc('season')->first();

现在我拥有当前的Season模型,我将hasMany Matches关系与Season一起使用,以返回所有即将到来的Matches:

Now that I have the current Season model, I use the hasMany Matches relationship to the Season to return all of the Matches that are upcoming:

$season->matches->where('match_date_time','>=', date('Y-m-d hh:ii:ss'));

现在我有一个匹配项.

当我在Matches集合中执行foreach任务时,我可以访问每个Match Model,然后使用hasMany Team关系(团队通常是一组报名参加该比赛的玩家),返回所有符合以下条件的玩家:选择参加该游戏.

When i do a foreach on my Matches collection, I can access each Match Model and then use the hasMany Team relationship (Team is usually a group of players who have signed up to play that match) which returns all of the players who opted in for playing that game.

foreach($matches as $match) {
           $players = $match->players;
        }

本质上,从上面的$ players集合(包含所有参加比赛的玩家组成)中,我想知道的每个比赛是,如果该团队中存在已登录的用户(例如user_id = 3)是否匹配.这将使我能够整理前端,让玩家选择是选择加入"还是选择退出"游戏.它还可以防止用户选择已经注册的游戏.

Essentially, from the $players collection above (which consists of all players opted in to play a match), All I want to know for each Match is, if the logged in user (e.g user_id = 3) exists in that Team for that Match or not. This will allow me to sort out my front-end to let players choose whether to "opt in" or "opt out" of the game. It will also allow me to prevent a user from opting in for a game they're already signed up for.

非常感谢您的帮助.

编辑(版权归Gage LaFleur所有): 因此,我将代码重构为以下代码.

Edit (Credit to Gage LaFleur): So I've refactored my code to the following..

@if($match->players->contains('user_id', auth()->id()))
<td>
<form method="POST" action="{{ route('match.cancel-availability' , $club->url_extension) }}">
@csrf
@method('DELETE')
<input type="hidden" value="{{ $match->id }}" name="match_id">
<button type="submit" class="btn btn-outline-success">Cancel Availability</button>
</form>
</td>
@else
<td>
<form method="POST" action="{{ route('match.submit-availability' , $club->url_extension) }}">
@csrf
<input type="hidden" value="{{ $match->id }}" name="match_id">
<button type="submit" class="btn btn-outline-success">Submit Availability</button>
</form>
</td>
@endif

这在刀片中是可以接受的"吗?我乐于接受更好的方法,谢谢大家到目前为止的帮助.

Is this "acceptable" in a blade? I'm open to better approaches, thanks all for your help so far.

此外,我刚刚注意到我实际上需要访问团队模型"表(本质上是该球员),因为该表包含诸如他们所在的团队,是否已经付款,他们进球的目标等值. ..

Also, I've just noticed that i actually need to access the Model of the Team table (essentially that player) as it contains values such as the team theyre on, if they've paid, goals they've scored etc..

推荐答案

我建议在玩家集合中使用contains():

I would suggest using contains() on the players collection:

if ($players->contains('id', Auth::id())){ }

这篇关于使用嵌套关系时,如何返回单个模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆