Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kxh's avatar
Level 1

Tournament System

Hello world,

Im trying to code a tournament system in laravel where users can join a tournament as single player or with teams and after that to generate brackets. Does anybody has some info for me, where can I start

Thanks in advance

0 likes
10 replies
kxh's avatar
Level 1

Hello, thank you for the response Right now I'm stuck on making the users join a created tournament, any ideas on that?

tykus's avatar

This could be solved with a many-to-many relationship, i.e. a User can enter many Tournaments and a Tournament can have many Users. Laravel conventions would expect a tournament_user pivot table name having user_id and tournament_id FK columns; and a BelongsToMany relation defined on both models

// User
public function tournaments()
{
	return $this->belongsToMany(Tournament::class);
}
// Topurnament
public function users()
{
	return $this->belongsToMany(User::class);
}
kxh's avatar
Level 1

Thank you for your answer I have the relationships defined and the pivot table created The part I'm struggling is how to make the button in the blade for users to join, do I need a function in the tournament controller?

tykus's avatar
tykus
Best Answer
Level 104

If you follow a Cruddy By Design approach, then a separate controller would be used, e.g. TournamentEntriesController. Define a route to register the user in the tournament:

Route::post('tournaments/{tournament}/entries', [TournamentEntriesController::class, 'store'])->name('tournament_entries.store');

In that controller, you can define a store method which accepts the tournament id:

// TournamentEntriesController
public function store(Request $request, Tournament $tournament)
{
	// If the authenticated user is registering for the tournament
	$tournament->users()->attach(auth()->id());

	return back();
}

The button needs only to be wrapped in a form:

<form action="{{ route('tournament_entries.store', $tournament) }}" method="POST">
	@csrf
	<button>Add</button>
</form>

This example assumes an authenticated user is joining a tournament; if you need to pick a User to assign to a tournament, then add a user_id field to the form.

kxh's avatar
Level 1

Thank you so much, you've been very helpful One thing that happens after I did this is that it shows an error of Undefined variable: tournament in the view

tykus's avatar

How are you displaying the Tournament that the User can join - I assumed you had a Tournament instance? Can you show the Controller action and View relevant to displaying the form?

kxh's avatar
Level 1

It was a mistake of mine, i forgot the foreach loop where I show the tournaments, I fixed it now Thank you so much for the help and time today

1 like

Please or to participate in this conversation.