CodingAuthority
BelongsToMany Relationship Cast Problem on Edit Record
Than i have UsersRelationManager for ProjectResource
That's all.
I am really sorry for sending more than few messages, discord limits the max characters
class UsersRelationManager extends RelationManager
{
protected static string $relationship = 'users';
public function isReadOnly(): bool
{
return false;
}
public function form(Form $form): Form
{
return $form
->schema([
Forms\Components\TextInput::make('email')
->required()
->maxLength(255),
Forms\Components\TextInput::make('special_cpi')
->required()
->numeric()
->inputMode('decimal'),
Forms\Components\Select::make('special_status')
->options(ProjectStatus::class)
->required()
]);
}
public function table(Table $table): Table
{
return $table
->recordTitleAttribute('email')
->columns([
Tables\Columns\TextColumn::make('email'),
Tables\Columns\TextColumn::make('special_cpi')
->money('USD'),
])
->filters([
//
])
->headerActions([
Tables\Actions\CreateAction::make(),
Tables\Actions\AttachAction::make(),
])
->actions([
Tables\Actions\EditAction::make(),
Tables\Actions\DetachAction::make(),
Tables\Actions\DeleteAction::make(),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DetachBulkAction::make(),
Tables\Actions\DeleteBulkAction::make(),
]),
]);
}
}
class UsersRelationManager extends RelationManager
{
protected static string $relationship = 'users';
public function isReadOnly(): bool
{
return false;
}
public function form(Form $form): Form
{
return $form
->schema([
Forms\Components\TextInput::make('email')
->required()
->maxLength(255),
Forms\Components\TextInput::make('special_cpi')
->required()
->numeric()
->inputMode('decimal'),
Forms\Components\Select::make('special_status')
->options(ProjectStatus::class)
->required()
]);
}
public function table(Table $table): Table
{
return $table
->recordTitleAttribute('email')
->columns([
Tables\Columns\TextColumn::make('email'),
Tables\Columns\TextColumn::make('special_cpi')
->money('USD'),
])
->filters([
//
])
->headerActions([
Tables\Actions\CreateAction::make(),
Tables\Actions\AttachAction::make(),
])
->actions([
Tables\Actions\EditAction::make(),
Tables\Actions\DetachAction::make(),
Tables\Actions\DeleteAction::make(),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DetachBulkAction::make(),
Tables\Actions\DeleteBulkAction::make(),
]),
]);
}
}
6 replies
BelongsToMany Relationship Cast Problem on Edit Record
Project Model has Cast and BelongsToMany Relationship
User Model has BelongsToMany Relationship
ProjectUser Model with Casts and other table related data
class Project extends Model
{
protected $casts = [
'status' => ProjectStatus::class,
'cpi' => MoneyCast::class,
];
public function users(): BelongsToMany
{
return $this->belongsToMany(User::class)
->using(ProjectUser::class)
->withPivot('pivot_id', 'special_status', 'special_cpi')
->withTimestamps();
}
}
class Project extends Model
{
protected $casts = [
'status' => ProjectStatus::class,
'cpi' => MoneyCast::class,
];
public function users(): BelongsToMany
{
return $this->belongsToMany(User::class)
->using(ProjectUser::class)
->withPivot('pivot_id', 'special_status', 'special_cpi')
->withTimestamps();
}
}
class User extends Authenticatable
{
public function projects(): BelongsToMany
{
return $this->belongsToMany(Project::class)
->using(ProjectUser::class)
->withPivot('pivot_id', 'special_status', 'special_cpi')
->withTimestamps();
}
}
class User extends Authenticatable
{
public function projects(): BelongsToMany
{
return $this->belongsToMany(Project::class)
->using(ProjectUser::class)
->withPivot('pivot_id', 'special_status', 'special_cpi')
->withTimestamps();
}
}
class ProjectUser extends Pivot
{
protected $primaryKey = 'pivot_id';
public $incrementing = true;
protected $table = 'project_user';
protected $casts = [
'special_status' => ProjectStatus::class,
'special_cpi' => MoneyCast::class,
];
}
class ProjectUser extends Pivot
{
protected $primaryKey = 'pivot_id';
public $incrementing = true;
protected $table = 'project_user';
protected $casts = [
'special_status' => ProjectStatus::class,
'special_cpi' => MoneyCast::class,
];
}
6 replies