0

I have a piece of code that I think is quite improvable.

In a DomainResource in the form() method, I have a circular relationship. (the only way I saw viable since I don't like the current ideas about having two guards in Filament)

The model has a relationship created with the User, but there are users that are not eligible.

So remove the //->relationship('user','name') option

The code works for me but the logged in user with the ability to create more models can write (even if it doesn't work) in the field

$user = auth()->user();
return $form
   ->Select::make('user_id')
       //->relationship('user','name')
       ->label('Administrador')
       ->options(function () use ($user){
           if ($user->getRoleNames()->first() === 'super-admin') {
               return  User::role('owner')->pluck('name', 'id');
           }
           return User::where('id', $user->id)->pluck('name', 'id');
       })
       ->searchable()

Any ideas?

After some time, I rewrote it a little better.

Select::make('user_id')
    //->relationship('user','name')
    ->label('Administrador')
    ->options(function () {
        if (auth()->user()->getRoleNames()->first() === 'super-admin')
        {
            return  User::role('owner')->pluck('name', 'id');
        }
        return collect([
            ['name' => auth()->user()->name, 'id' => auth()->user()->id]
        ])->pluck('name', 'id');
    })
    ->required()
    ->searchable()

Also, on resource class

public static function getEloquentQuery(): Builder
{
    if (auth()->user()->hasAnyRole(['super-admin'])) {
        return parent::getEloquentQuery()
            ->withoutGlobalScopes([
                SoftDeletingScope::class,
            ]);
    } else {
        return parent::getEloquentQuery()
            ->withoutGlobalScopes([
                SoftDeletingScope::class,
            ])
            ->whereBelongsTo(auth()->user());
    }
}

It works, but of course, the combo list of the relationship comes out empty instead of filled with the value of the logged in user, and active. Come on, it's not very appropriate.

abkrim
  • 3,512
  • 7
  • 43
  • 69
  • 1
    Try replacing `getRoleNames` with `hasAnyRole` and check. The Second one. ***After some time, I rewrote it a little better.*** – Abdulla Nilam Dec 21 '22 at 05:49
  • Thanks @AbdullaNilam ofr tip about `hasAnyRole`. But I'm looking for a way different for write this piece of code, not minor changes. Also, another problem is not resolved. Is not filled, and can't use default value when user is not super-admin. – abkrim Dec 21 '22 at 08:06
  • what you mean by this *the combo list of the relationship comes out empty* and do you only need to reformat/optimize the code? – Abdulla Nilam Dec 21 '22 at 08:21
  • ![Imagen](https://multimedia.castris.com/imagenes/posts/Combolist720.gif) Administrador combolist, must be deactivate, and default value must be Admin1. I think that wya code is not propper code (But it works) – abkrim Dec 21 '22 at 17:28

1 Answers1

0

try adding default this way.

->default(auth()->user()->hasRole('super-admin')?null:auth()->user()->id)