Lucky0
Lucky0
FFilament
Created by Lucky0 on 6/30/2024 in #❓┊help
Toggle with eager loading in form
Why eager load works in table but not form? UserModel
public function notificationPreference(): HasOne
{
return $this->hasOne(UserNotificationPreference::class);
}
public function notificationPreference(): HasOne
{
return $this->hasOne(UserNotificationPreference::class);
}
UserResource
Forms\Components\Toggle::make('notificationPreference.is_enabled')
Forms\Components\Toggle::make('notificationPreference.is_enabled')
Is there any alternatives?
5 replies
FFilament
Created by Lucky0 on 6/7/2024 in #❓┊help
ManyToMany Relationship on AttachAction
I am trying to use AttachAction, and I am trying to implement it. But instead i got an error error:
Call to undefined method App\Models\Car\Feature::carOwners()
Call to undefined method App\Models\Car\Feature::carOwners()
model:
namespace App\Models\Car;

class Feature extends Model
{
public function carFeatures(): BelongsToMany
{
return $this->belongsToMany(CarOwner::class, 'car_features')
->withPivot(['is_active', 'remark'])
->withTimestamps();
}
}
namespace App\Models\Car;

class Feature extends Model
{
public function carFeatures(): BelongsToMany
{
return $this->belongsToMany(CarOwner::class, 'car_features')
->withPivot(['is_active', 'remark'])
->withTimestamps();
}
}
namespace App\Models;

class CarFeature extends Model
{
public function feature(): BelongsTo
{
return $this->belongsTo(CarSpec\Feature::class);
}

public function carOwner(): BelongsTo
{
return $this->belongsTo(CarOwner::class);
}
}
namespace App\Models;

class CarFeature extends Model
{
public function feature(): BelongsTo
{
return $this->belongsTo(CarSpec\Feature::class);
}

public function carOwner(): BelongsTo
{
return $this->belongsTo(CarOwner::class);
}
}
namespace App\Models;

class CarOwner extends Model
{
public function carFeatures(): BelongsToMany
{
return $this->belongsToMany(CarSpec\Feature::class, 'car_features')
->withPivot(['is_active', 'remark'])
->withTimestamps();
}
}
namespace App\Models;

class CarOwner extends Model
{
public function carFeatures(): BelongsToMany
{
return $this->belongsToMany(CarSpec\Feature::class, 'car_features')
->withPivot(['is_active', 'remark'])
->withTimestamps();
}
}
namespace App\Filament\Driver\Resources\CarOwnerResource\RelationManagers;

class CarFeaturesRelationManager extends RelationManager
{
protected static string $relationship = 'carFeatures';

public function table(Table $table): Table
{
return $table
->recordTitleAttribute('name')
->columns([
...
}),
])
->headerActions([
Tables\Actions\AttachAction::make()
->preloadRecordSelect(),
// ->form(fn (Tables\Actions\AttachAction $attachAction): array => [
// $attachAction->getRecordSelect(),
// Forms\Components\Select::make('name'),
// ]),
])
}
}
namespace App\Filament\Driver\Resources\CarOwnerResource\RelationManagers;

class CarFeaturesRelationManager extends RelationManager
{
protected static string $relationship = 'carFeatures';

public function table(Table $table): Table
{
return $table
->recordTitleAttribute('name')
->columns([
...
}),
])
->headerActions([
Tables\Actions\AttachAction::make()
->preloadRecordSelect(),
// ->form(fn (Tables\Actions\AttachAction $attachAction): array => [
// $attachAction->getRecordSelect(),
// Forms\Components\Select::make('name'),
// ]),
])
}
}
5 replies
FFilament
Created by Lucky0 on 4/22/2024 in #❓┊help
External API CRUD in Filament
I am using "https://github.com/kawax/laravel-google-sheets" and "https://github.com/calebporzio/sushi". At the moment I can fetch the data from google sheets. I followed these two source "https://laraveldaily.com/post/filament-load-table-data-from-3rd-party-api" and "https://filamentphp.com/community/how-to-consume-an-external-api-with-filament-tables". I could not find any other reference for update, create and delete. Is it possible to do this in filament? do I need to create custom livewire class? currently I have this for retrieving the data from google sheets
namespace App\Models\Sheets;

use Revolution\Google\Sheets\Facades\Sheets;
use Illuminate\Database\Eloquent\Model;
use Sushi\Sushi;

class User extends Model
{
use Sushi;

protected $guarded = [
'id',
];

public function getRows(): array
{
$rows = Sheets::spreadsheet(config('google.sheets_api'))->sheet('sheet1')->get();

$header = $rows->pull(0);
$values = Sheets::collection(header: $header, rows: $rows);

$data = array_values($values->map(function ($row) {
return collect($row)->mapWithKeys(function ($value, $key) {
if (in_array($key, ['id', 'age'])) {
return [$key => intval($value)];
}
return [$key => $value];
});
})->toArray());

return $data;
}

}
namespace App\Models\Sheets;

use Revolution\Google\Sheets\Facades\Sheets;
use Illuminate\Database\Eloquent\Model;
use Sushi\Sushi;

class User extends Model
{
use Sushi;

protected $guarded = [
'id',
];

public function getRows(): array
{
$rows = Sheets::spreadsheet(config('google.sheets_api'))->sheet('sheet1')->get();

$header = $rows->pull(0);
$values = Sheets::collection(header: $header, rows: $rows);

$data = array_values($values->map(function ($row) {
return collect($row)->mapWithKeys(function ($value, $key) {
if (in_array($key, ['id', 'age'])) {
return [$key => intval($value)];
}
return [$key => $value];
});
})->toArray());

return $data;
}

}
10 replies
FFilament
Created by Lucky0 on 4/14/2024 in #❓┊help
Adding tooltip for each badge in a table column
I couldn't figure out to make this function work. is it possible to do this? what i have so far:
Tables\Columns\TextColumn::make('carFeatures.name')
->badge()
->tooltip(function (Tables\Columns\TextColumn $column, $record): string {
$states = $column->getState();

// dd($record->carFeatures->map(fn($query) => $query->description));

$tooltipContent = [];
if ($states !== null) {
foreach ($states as $state) {
$tooltipContent[] = $state;
}
} else {
$tooltipContent[] = null;
}
return implode(', ', $tooltipContent);
})
->listWithLineBreaks()
->limitList(2)
->expandableLimitedList()
->sortable(),
Tables\Columns\TextColumn::make('carFeatures.name')
->badge()
->tooltip(function (Tables\Columns\TextColumn $column, $record): string {
$states = $column->getState();

// dd($record->carFeatures->map(fn($query) => $query->description));

$tooltipContent = [];
if ($states !== null) {
foreach ($states as $state) {
$tooltipContent[] = $state;
}
} else {
$tooltipContent[] = null;
}
return implode(', ', $tooltipContent);
})
->listWithLineBreaks()
->limitList(2)
->expandableLimitedList()
->sortable(),
currently it will display all the features name when i hover into the feature column. but what i wanted is to display the feature's description for each of the badge if i hover onto them. please guide me.. thank you
3 replies
FFilament
Created by Lucky0 on 4/7/2024 in #❓┊help
deep relationship
I have 3 models User, Car, and Brand and the relationship is User > Car > Brand I created a UserResource and I want to fetch the brand name
Tables\Columns\TextColumn::make('car')
->formatStateUsing(fn(Car $car): string => $car->brand->name)
->sortable(),
Tables\Columns\TextColumn::make('car')
->formatStateUsing(fn(Car $car): string => $car->brand->name)
->sortable(),
with the code above i get "Attempt to read property "name" on null"
5 replies
FFilament
Created by Lucky0 on 4/1/2024 in #❓┊help
I can't select the first index. I've been banging my head for hours.
No description
5 replies
FFilament
Created by Lucky0 on 3/24/2024 in #❓┊help
UnitTest (bug)?
I am wondering why the code below only works for sorting 'name' but not 'email'.
use App\Filament\Admin\Resources\UserResource;
use App\Models\User;
use function Pest\Livewire\livewire;

describe(
'can sort users by: ',
fn() => collect([
'name',
'email'
])->each(function ($column) {
test($column, function () use ($column) {
$users = User::get();

livewire(UserResource\Pages\ListUsers::class)
->sortTable($column)
->assertCanSeeTableRecords($users->sortBy($column), inOrder: true)
->sortTable($column, 'desc')
->assertCanSeeTableRecords($users->sortByDesc($column), inOrder: true);
});
})
);
use App\Filament\Admin\Resources\UserResource;
use App\Models\User;
use function Pest\Livewire\livewire;

describe(
'can sort users by: ',
fn() => collect([
'name',
'email'
])->each(function ($column) {
test($column, function () use ($column) {
$users = User::get();

livewire(UserResource\Pages\ListUsers::class)
->sortTable($column)
->assertCanSeeTableRecords($users->sortBy($column), inOrder: true)
->sortTable($column, 'desc')
->assertCanSeeTableRecords($users->sortByDesc($column), inOrder: true);
});
})
);
UserResource:
public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('name')
->sortable()
->searchable(),
Tables\Columns\TextColumn::make('email')
->sortable()
->searchable(),
public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('name')
->sortable()
->searchable(),
Tables\Columns\TextColumn::make('email')
->sortable()
->searchable(),
43 replies
FFilament
Created by Lucky0 on 3/1/2024 in #❓┊help
Using filament theme on custom page
I am trying to use the Filament theme on a custom page. The custom page is using a Livewire component. I want to sync the color for the custom page and the panel. And yes, I have set up the custom theme with the "make:filament-theme" command.
<x-filament-panels::page>
<livewire:component/>
</x-filament-panels::page>
<x-filament-panels::page>
<livewire:component/>
</x-filament-panels::page>
livewire/components.blade.php


<div>
<h1 class="primay">{{ $hello }}</h1>
</div>
livewire/components.blade.php


<div>
<h1 class="primay">{{ $hello }}</h1>
</div>
9 replies
FFilament
Created by Lucky0 on 2/29/2024 in #❓┊help
Custom Table Layout
Is it possible to create a custom table, like a custom page? I tried to find the solution, but I couldn't find any information about it.
2 replies
FFilament
Created by Lucky0 on 2/28/2024 in #❓┊help
Badge N + 1 problem
As the title suggests, whenever I use getNavigationBadge() method, it somehow queries twice.
public static function getNavigationBadge(): ?string
{
return static::getModel()::count();
}
public static function getNavigationBadge(): ?string
{
return static::getModel()::count();
}
3 replies
FFilament
Created by Lucky0 on 2/20/2024 in #❓┊help
SelectColumn native
Hey, I'm trying to use "selectColumn" with native(), but it's not working. Do you know any other ways to make it work?
4 replies
FFilament
Created by Lucky0 on 2/16/2024 in #❓┊help
How to autofill recordId on TextInput in AttachAction
As the title suggests, undefined array key "recordId."
AttachAction::make()
->form(function () {
return [
TextInput::make('recordId'),
SelectTree::make('parent_id')
->relationship('parent', 'name', 'parent_id)
];
})
AttachAction::make()
->form(function () {
return [
TextInput::make('recordId'),
SelectTree::make('parent_id')
->relationship('parent', 'name', 'parent_id)
];
})
4 replies