jamesro
jamesro
FFilament
Created by jamesro on 10/17/2024 in #❓┊help
Relation manager with pivot table doesn't show pivot table field values when edit
sorted, change the relation manager form to this:
public function form(Form $form): Form
{
return $form
->schema([
Forms\Components\DateTimePicker::make('pivot_data_start')
->label('Data start')
->required(),
Forms\Components\DateTimePicker::make('pivot_data_end')
->required(),
Forms\Components\Toggle::make('pivot_disabled')
->mutateDehydratedStateUsing(function ($data) {
dd($data);
})
->inline(false)
->required(),
]);
}
public function form(Form $form): Form
{
return $form
->schema([
Forms\Components\DateTimePicker::make('pivot_data_start')
->label('Data start')
->required(),
Forms\Components\DateTimePicker::make('pivot_data_end')
->required(),
Forms\Components\Toggle::make('pivot_disabled')
->mutateDehydratedStateUsing(function ($data) {
dd($data);
})
->inline(false)
->required(),
]);
}
and now edit works just fine
2 replies
FFilament
Created by jamesro on 10/8/2024 in #❓┊help
HasManyThrough for select field
solved by changing relation to belongsToMany, hope is correctly:
public function categorii()
{
return $this->belongsToMany(Categorii::class, 'necesare_categorii', 'id_necesar', 'id_categorie');
}
public function categorii()
{
return $this->belongsToMany(Categorii::class, 'necesare_categorii', 'id_necesar', 'id_categorie');
}
3 replies
FFilament
Created by jamesro on 10/2/2024 in #❓┊help
FileUpload on relation ( HasOne) extra parameters
@Dan Harrin any suggestion?
7 replies
FFilament
Created by jamesro on 10/2/2024 in #❓┊help
FileUpload on relation ( HasOne) extra parameters
I'm expecting it to create the second relation only if I selected a file on that second field
7 replies
FFilament
Created by jamesro on 10/2/2024 in #❓┊help
FileUpload on relation ( HasOne) extra parameters
Fieldset::make('Adaugati un email')
->relationship('fisierEmail')
->mutateRelationshipDataBeforeCreateUsing(function ($data, Set $set, Get $get){
//if($data['fisier_aditional'] !== null) {
$data['user_add'] = auth()->id();
$data['data_add'] = now();
$data['tip'] = 2;
//}
return $data;
})
->schema([
FileUpload::make('fisier_aditional')
->acceptedFileTypes(['application/vnd.ms-outlook'])
->hiddenLabel()
->downloadable()
->columnSpan('full')
->directory('emails/comenzi')
->visibility('public')
->placeholder('Numai fisiere cu extensia .msg')
]),
Fieldset::make('Adaugati un email')
->relationship('fisierEmail')
->mutateRelationshipDataBeforeCreateUsing(function ($data, Set $set, Get $get){
//if($data['fisier_aditional'] !== null) {
$data['user_add'] = auth()->id();
$data['data_add'] = now();
$data['tip'] = 2;
//}
return $data;
})
->schema([
FileUpload::make('fisier_aditional')
->acceptedFileTypes(['application/vnd.ms-outlook'])
->hiddenLabel()
->downloadable()
->columnSpan('full')
->directory('emails/comenzi')
->visibility('public')
->placeholder('Numai fisiere cu extensia .msg')
]),
7 replies
FFilament
Created by jamesro on 10/2/2024 in #❓┊help
FileUpload on relation ( HasOne) extra parameters
What am I doing wrong here? why is filament trying to save my second upload field relation even if I don't selected a file in that field?
Fieldset::make('Adaugati un fisier')
->relationship('fisierAditional')
->mutateRelationshipDataBeforeCreateUsing(function ($data, Set $set, Get $get){
$data['user_add'] = auth()->id();
$data['data_add'] = now();
$data['tip'] = 1;

return $data;
})
->schema([
FileUpload::make('fisier_aditional')
->hiddenLabel()
->downloadable()
->columnSpan('full')
->directory('pdfs/comenzi')
->visibility('public')
->placeholder('Numai imagini sau fisiere de tip PDF.')
->acceptedFileTypes(['application/pdf', 'image/*']),
]),
Fieldset::make('Adaugati un fisier')
->relationship('fisierAditional')
->mutateRelationshipDataBeforeCreateUsing(function ($data, Set $set, Get $get){
$data['user_add'] = auth()->id();
$data['data_add'] = now();
$data['tip'] = 1;

return $data;
})
->schema([
FileUpload::make('fisier_aditional')
->hiddenLabel()
->downloadable()
->columnSpan('full')
->directory('pdfs/comenzi')
->visibility('public')
->placeholder('Numai imagini sau fisiere de tip PDF.')
->acceptedFileTypes(['application/pdf', 'image/*']),
]),
7 replies
FFilament
Created by jamesro on 10/1/2024 in #❓┊help
Showing a modal with list of order products
Oky I found this on FilamentPhp documentation https://filamentphp.com/docs/3.x/tables/advanced#preventing-query-string-conflicts-with-the-pagination-page
public function table(Table $table): Table
{
return $table
->queryStringIdentifier('orderProducts');
}
public function table(Table $table): Table
{
return $table
->queryStringIdentifier('orderProducts');
}
i've set ->queryStringIdentifier('orderProducts'); and now all good
2 replies
FFilament
Created by jamesro on 9/25/2024 in #❓┊help
Add roles / permissions per team for user
this is what I've ended doing:
Forms\Components\Select::make('roles')
->preload()
->label('Roluri')
->relationship(name: 'roles', titleAttribute: 'name')
->saveRelationshipsUsing(function ($record, $state, $get) use ($form) {
setPermissionsTeamId($form->getRecord()->id);
$record->roles()->syncWithPivotValues($state, [config('permission.column_names.team_foreign_key') => $form->getRecord()->id]);
})
->multiple()
->preload()
->searchable(),
Forms\Components\Select::make('roles')
->preload()
->label('Roluri')
->relationship(name: 'roles', titleAttribute: 'name')
->saveRelationshipsUsing(function ($record, $state, $get) use ($form) {
setPermissionsTeamId($form->getRecord()->id);
$record->roles()->syncWithPivotValues($state, [config('permission.column_names.team_foreign_key') => $form->getRecord()->id]);
})
->multiple()
->preload()
->searchable(),
33 replies
FFilament
Created by jamesro on 9/25/2024 in #❓┊help
Add roles / permissions per team for user
managed to sort it in the end, even simpler :))
33 replies
FFilament
Created by jamesro on 9/25/2024 in #❓┊help
Add roles / permissions per team for user
@CodeWithDennis @Lara Zeus i've managed to eliminate that TeamUser, the question i have is, I want to have a super admin panel, where you can see resources from all Teams, the problem I see rising is this Spatie Permission relation
public function roles(): BelongsToMany
{
$relation = $this->morphToMany(
config('permission.models.role'),
'model',
config('permission.table_names.model_has_roles'),
config('permission.column_names.model_morph_key'),
app(PermissionRegistrar::class)->pivotRole
);

if (! app(PermissionRegistrar::class)->teams) {
return $relation;
}

$teamsKey = app(PermissionRegistrar::class)->teamsKey;
$relation->withPivot($teamsKey);
$teamField = config('permission.table_names.roles').'.'.$teamsKey;

return $relation->wherePivot($teamsKey, getPermissionsTeamId())
->where(fn ($q) => $q->whereNull($teamField)->orWhere($teamField, getPermissionsTeamId()));
}
public function roles(): BelongsToMany
{
$relation = $this->morphToMany(
config('permission.models.role'),
'model',
config('permission.table_names.model_has_roles'),
config('permission.column_names.model_morph_key'),
app(PermissionRegistrar::class)->pivotRole
);

if (! app(PermissionRegistrar::class)->teams) {
return $relation;
}

$teamsKey = app(PermissionRegistrar::class)->teamsKey;
$relation->withPivot($teamsKey);
$teamField = config('permission.table_names.roles').'.'.$teamsKey;

return $relation->wherePivot($teamsKey, getPermissionsTeamId())
->where(fn ($q) => $q->whereNull($teamField)->orWhere($teamField, getPermissionsTeamId()));
}
as you se it relies on getPermissionsTeamId() which you normally set when you switch the team, any idea how I can make it work in super admin panel so it brings the correct things?
33 replies
FFilament
Created by jamesro on 9/25/2024 in #❓┊help
Add roles / permissions per team for user
yeeye i think i sorted it, now it saves correctly my
->saveRelationshipsBeforeChildrenUsing(function ($component, $set, $get, $record) {
$record->load('users.roles');
$rolesArray = [];
foreach ($get('userss') as $userTeam) {

$rolesArray[$userTeam['id']] = $userTeam['roles'];
}

// Sync roles with team_id
$record->users()->each(function (TeamUser $teamUser) use ($rolesArray) {

$teamUser->roles()->syncWithPivotValues($rolesArray[$teamUser->id], ['team_id' => $teamUser->team_id, 'model_id' => $teamUser->id]);
});

})
->saveRelationshipsBeforeChildrenUsing(function ($component, $set, $get, $record) {
$record->load('users.roles');
$rolesArray = [];
foreach ($get('userss') as $userTeam) {

$rolesArray[$userTeam['id']] = $userTeam['roles'];
}

// Sync roles with team_id
$record->users()->each(function (TeamUser $teamUser) use ($rolesArray) {

$teamUser->roles()->syncWithPivotValues($rolesArray[$teamUser->id], ['team_id' => $teamUser->team_id, 'model_id' => $teamUser->id]);
});

})
replaced saveRelationshipsUsing with saveRelationshipsBeforeChildrenUsing
33 replies
FFilament
Created by jamesro on 9/25/2024 in #❓┊help
Add roles / permissions per team for user
@CodeWithDennis @Lara Zeus any idea ? 😢
33 replies
FFilament
Created by jamesro on 9/25/2024 in #❓┊help
Add roles / permissions per team for user
i tried this also
->saveRelationshipsUsing(function ($component, $set, $get, $record) {
$record->load('users.roles');
$rolesArray = [];

foreach ($get('userss') as $userTeam) {
$rolesArray[$userTeam['id']] = $userTeam['roles'];
}

// Sync roles with team_id
$record->users()->each(function (TeamUser $teamUser) use ($rolesArray) {
$teamUser->roles()->syncWithPivotValues($rolesArray[$teamUser->id], ['team_id' => $teamUser->team_id, 'model_id' => $teamUser->id]);
});

})
->saveRelationshipsUsing(function ($component, $set, $get, $record) {
$record->load('users.roles');
$rolesArray = [];

foreach ($get('userss') as $userTeam) {
$rolesArray[$userTeam['id']] = $userTeam['roles'];
}

// Sync roles with team_id
$record->users()->each(function (TeamUser $teamUser) use ($rolesArray) {
$teamUser->roles()->syncWithPivotValues($rolesArray[$teamUser->id], ['team_id' => $teamUser->team_id, 'model_id' => $teamUser->id]);
});

})
but no joy, somehow the model_has_roles is getting the values inserted without team_id before even to reach my each statement and I get sql duplicat id error
33 replies
FFilament
Created by jamesro on 9/25/2024 in #❓┊help
Add roles / permissions per team for user
array:1 [// app/Filament/Resources/TeamResource.php:73
"record-68" => array:6 [
"id" => 68
"team_id" => 1
"user_id" => 236
"created_at" => "2024-09-26T09:15:09.000000Z"
"updated_at" => "2024-09-26T09:15:09.000000Z"
"roles" => array:1 [
0 => "1"
]
]
]
array:1 [// app/Filament/Resources/TeamResource.php:73
"record-68" => array:6 [
"id" => 68
"team_id" => 1
"user_id" => 236
"created_at" => "2024-09-26T09:15:09.000000Z"
"updated_at" => "2024-09-26T09:15:09.000000Z"
"roles" => array:1 [
0 => "1"
]
]
]
managed to catch something when I did
->saveRelationshipsUsing(function ($component, $set, $get, $record) {
dd($get('userss'));
})
->saveRelationshipsUsing(function ($component, $set, $get, $record) {
dd($get('userss'));
})
33 replies
FFilament
Created by jamesro on 9/25/2024 in #❓┊help
Add roles / permissions per team for user
@Lara Zeus using saveRelationshipsUsing on the repeater, does nothing trying to place a dd right after
->saveRelationshipsUsing(function ($component, $set, $get, $record) {
dd('die');
})
->saveRelationshipsUsing(function ($component, $set, $get, $record) {
dd('die');
})
it goes by and inserts a value in model_has_roles with team_id missing
33 replies
FFilament
Created by jamesro on 9/25/2024 in #❓┊help
Add roles / permissions per team for user
from what I see the problem starts here:
Forms\Components\Repeater::make('userss')
->label(function () {
return 'Members';
})
->columnSpan('2')
->relationship('users') // Assuming there's a relationship for products
->schema([
Forms\Components\Grid::make(3) // Defines a grid with 4 columns
->schema([
Select::make('user_id')
->label('Users')
->relationship('user', 'nume')
->required(),
Select::make('roles')
->label('Roles')
->multiple()
->preload()
->relationship('roles', 'name')
])
])
Forms\Components\Repeater::make('userss')
->label(function () {
return 'Members';
})
->columnSpan('2')
->relationship('users') // Assuming there's a relationship for products
->schema([
Forms\Components\Grid::make(3) // Defines a grid with 4 columns
->schema([
Select::make('user_id')
->label('Users')
->relationship('user', 'nume')
->required(),
Select::make('roles')
->label('Roles')
->multiple()
->preload()
->relationship('roles', 'name')
])
])
i saves me the roles in the model_has_roles table, but it doesn't insert the team_id value, which is actually the record->id that i'm currently editing..
33 replies
FFilament
Created by jamesro on 9/25/2024 in #❓┊help
Add roles / permissions per team for user
do you guys think is oky this relation I have in my User mode?
public function teams(): BelongsToMany
{
return $this->belongsToMany(Team::class, 'team_user')->using(TeamUser::class);
}
public function teams(): BelongsToMany
{
return $this->belongsToMany(Team::class, 'team_user')->using(TeamUser::class);
}
do I really need that TeamUser ?
33 replies
FFilament
Created by jamesro on 9/25/2024 in #❓┊help
Add roles / permissions per team for user
@Lara Zeus i get no luck I get this error when trying to save SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '1-52-App\Models\TeamUser' for key 'PRIMARY' (Connection: mysql, SQL: insert into model_has_roles (model_id, model_type, role_id) values (52, App\Models\TeamUser, 1)) this is my form
public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\TextInput::make('name')
->required()
->maxLength(255),
Forms\Components\TextInput::make('slug')
->required()
->maxLength(255),


Forms\Components\Repeater::make('users')
->label(function () {
return 'Members';
})
->columnSpan('2')
->relationship('users')
->schema([
Forms\Components\Grid::make(3) // Defines a grid with 4 columns
->schema([
Select::make('user_id')
->label('Users')
->relationship('user', 'nume')
->required(),
Select::make('roles')
->label('Roles')
->multiple()
->preload()
->relationship('roles', 'name')
])
])
->addActionLabel('Add member'),
]);
}
public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\TextInput::make('name')
->required()
->maxLength(255),
Forms\Components\TextInput::make('slug')
->required()
->maxLength(255),


Forms\Components\Repeater::make('users')
->label(function () {
return 'Members';
})
->columnSpan('2')
->relationship('users')
->schema([
Forms\Components\Grid::make(3) // Defines a grid with 4 columns
->schema([
Select::make('user_id')
->label('Users')
->relationship('user', 'nume')
->required(),
Select::make('roles')
->label('Roles')
->multiple()
->preload()
->relationship('roles', 'name')
])
])
->addActionLabel('Add member'),
]);
}
33 replies
FFilament
Created by jamesro on 9/25/2024 in #❓┊help
Add roles / permissions per team for user
No description
33 replies
FFilament
Created by jamesro on 9/25/2024 in #❓┊help
Add roles / permissions per team for user
The sql error is solved but team_id field remains empty
33 replies