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..
Could you check if the user is in the current match with an
in_array()check in your foreach?There are other, probably cleaner ways to go about this, but the above is probably the easiest to drop in. If you're interested, I would suggest going about this the other way. Looping instead over the players deep relationship to the club.
Since I'm not 100% sure what data you are trying to retrieve, I'm going to assume its something like this:
CLUB -> Season (possibly variable):
and you have an authenticated user who is able to be matched with one of those
Playermodels in that tree. This comes with the constraint that the user should be able to see matches they are involved in, the ability to sign up for new matches, and the ability to un-sign up for matches they've already signed up for.