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.

4
  • 1
    Try replacing getRoleNames with hasAnyRole and check. The Second one. After some time, I rewrote it a little better. Commented Dec 21, 2022 at 5: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. Commented Dec 21, 2022 at 8: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? Commented Dec 21, 2022 at 8:21
  • !Imagen Administrador combolist, must be deactivate, and default value must be Admin1. I think that wya code is not propper code (But it works) Commented Dec 21, 2022 at 17:28

1 Answer 1

1

try adding default this way.

->default(auth()->user()->hasRole('super-admin')?null:auth()->user()->id)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.