Mar 13, 2024
0
Level 3
How to customize which field is displayed on Attach dropdown
Hi guys.
I create a relation manager named SubscriptionsRelationManager.
That relationship is between DepartmentSchedule and Subscription. It works great but, when the attach form appear it showing the subscription.id but I want to show subscription.user.name . How can I do that?
Screenshot of the attach form: https://share.cleanshot.com/6pPTtcKW
The relation manager
<?php
namespace App\Filament\Resources\DepartmentScheduleResource\RelationManagers;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\RelationManagers\RelationManager;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletingScope;
class SubscriptionsRelationManager extends RelationManager
{
protected static string $relationship = 'subscriptions';
public function form(Form $form): Form
{
return $form
->schema([
Forms\Components\TextInput::make('id')
->placeholder('ID asdf')
->required()
->maxLength(255),
]);
}
public function table(Table $table): Table
{
return $table
->recordTitleAttribute('id')
->columns([
Tables\Columns\TextColumn::make('id'),
])
->filters([
Tables\Filters\TrashedFilter::make()
])
->headerActions([
Tables\Actions\CreateAction::make(),
Tables\Actions\AttachAction::make()
->preloadRecordSelect()
->using(function (array $data, Model $record) {
$this->getOwnerRecord()->subscriptions()->attach($record, [
'created_by_id' => auth()->id(),
]);
}),
])
->actions([
Tables\Actions\EditAction::make(),
Tables\Actions\DetachAction::make(),
Tables\Actions\DeleteAction::make(),
Tables\Actions\ForceDeleteAction::make(),
Tables\Actions\RestoreAction::make(),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DetachBulkAction::make(),
Tables\Actions\DeleteBulkAction::make(),
Tables\Actions\RestoreBulkAction::make(),
Tables\Actions\ForceDeleteBulkAction::make(),
]),
])
->modifyQueryUsing(fn (Builder $query) => $query->withoutGlobalScopes([
SoftDeletingScope::class,
]));
}
}
Subscription Model
<?php
namespace App\Models;
use App\Traits\Models\HasLoginManagement;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Subscription extends Model
{
use HasFactory;
use SoftDeletes;
protected $fillable = [
'subscription_status_id',
'event_package_id',
'user_id',
];
public function eventPackage()
{
return $this->belongsTo(EventPackage::class);
}
public function subscriptionStatus()
{
return $this->belongsTo(SubscriptionStatus::class);
}
public function user()
{
return $this->belongsTo(User::class);
}
public function departmentSchedules()
{
return $this->belongsToMany(
DepartmentSchedule::class,
'department_schedule_subscriptions',
'subscription_id',
'department_schedule_id'
)->withPivot('created_by_id', 'updated_by_id', 'user')
->withTimestamps();
}
}
DepartmentSchedule
<?php
namespace App\Models;
use App\Traits\Models\HasLoginManagement;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class DepartmentSchedule extends Model
{
use HasFactory;
use SoftDeletes;
use HasLoginManagement;
protected $fillable = [
'department_id',
'name',
'schedule_date',
'start_time',
'end_time',
'repeat',
'repeat_until',
'capacity',
'created_by_id',
'updated_by_id',
];
public function department()
{
return $this->belongsTo(Department::class);
}
public function createdBy()
{
return $this->belongsTo(User::class, 'created_by_id');
}
public function updatedBy()
{
return $this->belongsTo(User::class, 'updated_by_id');
}
public function subscriptions()
{
return $this->belongsToMany(
Subscription::class,
'department_schedule_subscriptions',
'department_schedule_id',
'subscription_id'
)->withPivot('created_by_id', 'updated_by_id')
->withTimestamps();
}
}
Please or to participate in this conversation.