mirror of
https://github.com/filamentphp/filament.git
synced 2026-09-24 15:42:09 +08:00
Merge branch '3.x' into laravel-11
This commit is contained in:
@@ -264,6 +264,21 @@ Select::make('author_id')
|
||||
->searchable(['first_name', 'last_name'])
|
||||
```
|
||||
|
||||
### Saving pivot data to the relationship
|
||||
|
||||
If you're using a `multiple()` relationship and your pivot table has additional columns, you can use the `pivotData()` method to specify the data that should be saved in them:
|
||||
|
||||
```php
|
||||
use Filament\Forms\Components\Select;
|
||||
|
||||
Select::make('primaryTechnologies')
|
||||
->relationship(name: 'technologies', titleAttribute: 'name')
|
||||
->multiple()
|
||||
->pivotData([
|
||||
'is_primary' => true,
|
||||
])
|
||||
```
|
||||
|
||||
### Creating a new option in a modal
|
||||
|
||||
You may define a custom form that can be used to create a new record and attach it to the `BelongsTo` relationship:
|
||||
|
||||
@@ -240,6 +240,20 @@ CheckboxList::make('authors')
|
||||
->getOptionLabelFromRecordUsing(fn (Model $record) => "{$record->first_name} {$record->last_name}")
|
||||
```
|
||||
|
||||
### Saving pivot data to the relationship
|
||||
|
||||
If your pivot table has additional columns, you can use the `pivotData()` method to specify the data that should be saved in them:
|
||||
|
||||
```php
|
||||
use Filament\Forms\Components\CheckboxList;
|
||||
|
||||
CheckboxList::make('primaryTechnologies')
|
||||
->relationship(name: 'technologies', titleAttribute: 'name')
|
||||
->pivotData([
|
||||
'is_primary' => true,
|
||||
])
|
||||
```
|
||||
|
||||
## Setting a custom no search results message
|
||||
|
||||
When you're using a searchable checkbox list, you may want to display a custom message when no search results are found. You can do this using the `noSearchResultsMessage()` method:
|
||||
|
||||
@@ -24,6 +24,7 @@ class CheckboxList extends Field implements Contracts\CanDisableOptions, Contrac
|
||||
use Concerns\HasGridDirection;
|
||||
use Concerns\HasNestedRecursiveValidationRules;
|
||||
use Concerns\HasOptions;
|
||||
use Concerns\HasPivotData;
|
||||
|
||||
/**
|
||||
* @var view-string
|
||||
@@ -185,7 +186,15 @@ class CheckboxList extends Field implements Contracts\CanDisableOptions, Contrac
|
||||
});
|
||||
|
||||
$this->saveRelationshipsUsing(static function (CheckboxList $component, ?array $state) {
|
||||
$component->getRelationship()->sync($state ?? []);
|
||||
$pivotData = $component->getPivotData();
|
||||
|
||||
if ($pivotData === []) {
|
||||
$component->getRelationship()->sync($state ?? []);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$component->getRelationship()->syncWithPivotValues($state ?? [], $pivotData);
|
||||
});
|
||||
|
||||
$this->dehydrated(false);
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace Filament\Forms\Components\Concerns;
|
||||
|
||||
use Closure;
|
||||
|
||||
trait HasPivotData
|
||||
{
|
||||
/**
|
||||
* @var array<string, mixed> | Closure
|
||||
*/
|
||||
protected array | Closure $pivotData = [];
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> | Closure $data
|
||||
*/
|
||||
public function pivotData(array | Closure $data): static
|
||||
{
|
||||
$this->pivotData = $data;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function getPivotData(): array
|
||||
{
|
||||
return $this->evaluate($this->pivotData) ?? [];
|
||||
}
|
||||
}
|
||||
@@ -46,6 +46,7 @@ class Select extends Field implements Contracts\CanDisableOptions, Contracts\Has
|
||||
use Concerns\HasLoadingMessage;
|
||||
use Concerns\HasNestedRecursiveValidationRules;
|
||||
use Concerns\HasOptions;
|
||||
use Concerns\HasPivotData;
|
||||
use Concerns\HasPlaceholder;
|
||||
use HasExtraAlpineAttributes;
|
||||
|
||||
@@ -963,7 +964,15 @@ class Select extends Field implements Contracts\CanDisableOptions, Contracts\Has
|
||||
return;
|
||||
}
|
||||
|
||||
$relationship->sync($state ?? []);
|
||||
$pivotData = $component->getPivotData();
|
||||
|
||||
if ($pivotData === []) {
|
||||
$relationship->sync($state ?? []);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$relationship->syncWithPivotValues($state ?? [], $pivotData);
|
||||
});
|
||||
|
||||
$this->createOptionUsing(static function (Select $component, array $data, Form $form) {
|
||||
|
||||
Reference in New Issue
Block a user