Select default relationship

<?php

namespace App\Filament\User\Pages;

use App\Enums\PaymentType;
use App\Models\Store;
use Filament\Facades\Filament;
use Filament\Forms\Components\DatePicker;
use Filament\Forms\Components\Select;
use Filament\Forms\Form;
use Filament\Pages\Dashboard as BaseDashboard;
use Filament\Pages\Dashboard\Concerns\HasFiltersForm;

class Dashboard extends BaseDashboard
{
use HasFiltersForm;

protected static ?string $navigationIcon = 'heroicon-o-home';
protected static ?string $title = 'Escritorio';
protected static string $view = 'filament-panels::pages.dashboard';

public function mount(): void
{
abort_unless(auth()->user()->hasRole('user'), 403);
}

public static function shouldRegisterNavigation(): bool
{
return auth()->user()->hasRole('user');
}

public function filtersForm(Form $form): Form
{
// dd(auth()->user()->stores()->pluck('name', 'id'));
return $form
->schema([
Select::make('stores')
->multiple()
->relationship('stores', 'name')
->searchable()
->preload()
->default([Filament::getTenant()->id])
->options(auth()->user()->stores()->pluck('name', 'id'))
->label('Tiendas'),
DatePicker::make('startDate')->default(now())->label('Desde'),
DatePicker::make('endDate')->default(now())->label('Hasta'),
Select::make('payment_method')->options(PaymentType::class)->label('Método de pago')
]);
}
}
<?php

namespace App\Filament\User\Pages;

use App\Enums\PaymentType;
use App\Models\Store;
use Filament\Facades\Filament;
use Filament\Forms\Components\DatePicker;
use Filament\Forms\Components\Select;
use Filament\Forms\Form;
use Filament\Pages\Dashboard as BaseDashboard;
use Filament\Pages\Dashboard\Concerns\HasFiltersForm;

class Dashboard extends BaseDashboard
{
use HasFiltersForm;

protected static ?string $navigationIcon = 'heroicon-o-home';
protected static ?string $title = 'Escritorio';
protected static string $view = 'filament-panels::pages.dashboard';

public function mount(): void
{
abort_unless(auth()->user()->hasRole('user'), 403);
}

public static function shouldRegisterNavigation(): bool
{
return auth()->user()->hasRole('user');
}

public function filtersForm(Form $form): Form
{
// dd(auth()->user()->stores()->pluck('name', 'id'));
return $form
->schema([
Select::make('stores')
->multiple()
->relationship('stores', 'name')
->searchable()
->preload()
->default([Filament::getTenant()->id])
->options(auth()->user()->stores()->pluck('name', 'id'))
->label('Tiendas'),
DatePicker::make('startDate')->default(now())->label('Desde'),
DatePicker::make('endDate')->default(now())->label('Hasta'),
Select::make('payment_method')->options(PaymentType::class)->label('Método de pago')
]);
}
}
But in no Select field it sets the default value I want, except in startDate and endDate, which do have it by default. What could be happening?
4 Replies
krekas
krekas8mo ago
in mount() I think you miss the $this->form->fill();
Daniel Reales
Daniel RealesOP8mo ago
thank you so much! It's works now But If I change between tenants always keep the first Tenant default: Select::make('stores') ->multiple() ->relationship('stores', 'name') ->searchable() ->preload() ->default([Filament::getTenant()->id]) ->options(auth()->user()->stores()->pluck('name', 'id')) ->label('Tiendas'),
krekas
krekas8mo ago
what do you always keep first tenant default?
Daniel Reales
Daniel RealesOP8mo ago
Exactly, now all queries are saved in session even if the Tenant changes Now that I think about it, it could even stay that way. jeje
Want results from more Discord servers?
Add your server