Skip to content

Commit

Permalink
Merge pull request #183 from ploi-deploy/feature/users-projects
Browse files Browse the repository at this point in the history
Ability to add a project to a user via the user resource
  • Loading branch information
Cannonb4ll authored Oct 25, 2022
2 parents 1b1d3e8 + 17f760d commit e480028
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 2 deletions.
2 changes: 2 additions & 0 deletions app/Filament/Resources/UserResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Filament\Resources;

use App\Models\Project;
use Filament\Forms;
use App\Models\User;
use Filament\Tables;
Expand Down Expand Up @@ -57,6 +58,7 @@ public static function getRelations(): array
RelationManagers\ItemsRelationManager::class,
RelationManagers\CommentsRelationManager::class,
RelationManagers\VotesRelationManager::class,
RelationManagers\ProjectsRelationManager::class,
];
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace App\Filament\Resources\UserResource\RelationManagers;

use App\Models\Project;
use Filament\Forms;
use Filament\Resources\Form;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Resources\Table;
use Filament\Tables;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

class ProjectsRelationManager extends RelationManager
{
protected static string $relationship = 'projects';

protected static ?string $recordTitleAttribute = 'title';

public static function canViewForRecord(Model $ownerRecord): bool
{
return Project::query()->where('private', '=', true)->count() > 0;
}

public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\TextInput::make('name')
->required()
->maxLength(255),
]);
}

public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('title'),
])
->filters([
//
])
->headerActions([
Tables\Actions\AttachAction::make()
->recordSelectOptionsQuery(fn (Builder $query): Builder => $query->where('private', '=', true))
->recordSelect(fn (Forms\Components\Select $select) => $select->helperText(__('projects.select-hidden-projects')))
->inverseRelationshipName('members')
->preloadRecordSelect(),
])
->actions([
Tables\Actions\DetachAction::make(),
])
->bulkActions([
Tables\Actions\DetachBulkAction::make(),
]);
}
}
3 changes: 2 additions & 1 deletion lang/en/projects.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@

return [
'projects' => 'Projects',
'no-projects' => 'There are no projects.'
'no-projects' => 'There are no projects.',
'select-hidden-projects' => 'You can only select projects where there private option is enabled.',
];
3 changes: 2 additions & 1 deletion lang/nl/projects.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@

return [
'projects' => 'Projecten',
'no-projects' => 'Er zijn geen projecten.'
'no-projects' => 'Er zijn geen projecten.',
'select-hidden-projects' => 'Je kunt alleen projecten selecteren waar de private optie is ingeschakeld.',
];

0 comments on commit e480028

Please sign in to comment.