MrPaperbag
MrPaperbag
FFilament
Created by MrPaperbag on 5/7/2025 in #❓┊help
Create related resource with another relationship
class games {
date: date
finished: boolean
winner_id: bigint
created_at: timestamp(0)
updated_at: timestamp(0)
id: bigint
}
class rounds {
round_number: integer
game_id: bigint
winner_id: bigint
created_at: timestamp(0)
updated_at: timestamp(0)
id: bigint
}
class scores {
round_id: bigint
user_id: bigint
points: integer
created_at: timestamp(0)
updated_at: timestamp(0)
id: bigint
}
class users {
name: varchar(255)
surname: varchar(255)
email: varchar(255)
email_verified_at: timestamp(0)
password: varchar(255)
remember_token: varchar(100)
created_at: timestamp(0)
updated_at: timestamp(0)
id: bigint
}
class games {
date: date
finished: boolean
winner_id: bigint
created_at: timestamp(0)
updated_at: timestamp(0)
id: bigint
}
class rounds {
round_number: integer
game_id: bigint
winner_id: bigint
created_at: timestamp(0)
updated_at: timestamp(0)
id: bigint
}
class scores {
round_id: bigint
user_id: bigint
points: integer
created_at: timestamp(0)
updated_at: timestamp(0)
id: bigint
}
class users {
name: varchar(255)
surname: varchar(255)
email: varchar(255)
email_verified_at: timestamp(0)
password: varchar(255)
remember_token: varchar(100)
created_at: timestamp(0)
updated_at: timestamp(0)
id: bigint
}
<?php
// Game.php
public function rounds(): HasMany
{
return $this->hasMany(Round::class);
}
<?php
// Game.php
public function rounds(): HasMany
{
return $this->hasMany(Round::class);
}
<?php
// Round.php
public function game(): BelongsTo
{
return $this->belongsTo(Game::class);
}
<?php
// Round.php
public function game(): BelongsTo
{
return $this->belongsTo(Game::class);
}
<?php
// Score.php
public function round(): BelongsTo
{
return $this->belongsTo(Round::class);
}
<?php
// Score.php
public function round(): BelongsTo
{
return $this->belongsTo(Round::class);
}
<?php
// RoundsRelationManager.php

public function form(Form $form): Form
{
$schema = [];

$this->getRelationship()->getParent()->users->each(function ($user) use (&$schema) {
$schema[] = TextInput::make("scores.{$user->id}.points")
->label("Score for {$user->name}")
->numeric()
->required()
->minValue(0)
->default(0);
});

return $form->schema([
TextInput::make('round_number')
->label('Round Number')
->disabled()
->default(fn() => $this->getRelationship()->getParent()->rounds()->count() + 1),
...$schema,
]);
}
<?php
// RoundsRelationManager.php

public function form(Form $form): Form
{
$schema = [];

$this->getRelationship()->getParent()->users->each(function ($user) use (&$schema) {
$schema[] = TextInput::make("scores.{$user->id}.points")
->label("Score for {$user->name}")
->numeric()
->required()
->minValue(0)
->default(0);
});

return $form->schema([
TextInput::make('round_number')
->label('Round Number')
->disabled()
->default(fn() => $this->getRelationship()->getParent()->rounds()->count() + 1),
...$schema,
]);
}
2 replies