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:
The structure is as follows:
Each club is independant.
Club table (id, name, etc).
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
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:
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();
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'));
Now I have a collection of Matches.
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;
}
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.
Any help will be highly appreciated, thank you.
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..
If i've undestand correctly, the answer is pretty easy using hasManyThrough relationship. It allow you, if you have 3 tables A B C, and A has many B and B has many C, to get the C elements connected to A, and than of those records, you can select only the id and than check if the id exist in that collection