F
Filament2w ago
o.m

What caused the Collection::clone does not exist?

public static function getTableQuery()
{

$instance = new self();
$notifications = collect($instance->getNotificationSlugs());

$slugs = [];
foreach ($notifications as $notification) {
if (isset($notification['sms'])) {
$slugs[] = $notification['sms'];
}

if (isset($notification['email'])) {
$slugs[] = $notification['email'];
}
}

$options = Option::query()
->whereIn('slug', $slugs)
->get()
->pluck('updated_at', 'slug')
->toArray();

return $notifications->map(function ($slug) use ($options) {
$hasSms = isset($slug['sms']);
$hasEmail = isset($slug['email']);

$type = match(true) {
$hasSms && $hasEmail => 'Email, SMS',
$hasSms => 'SMS',
$hasEmail => 'Email',
};

$lastUpdated = match(true) {
$hasEmail => data_get($options, $slug['email']),
$hasSms => data_get($options, $slug['sms']),
default => null,
};

return [
'label' => $slug['label'],
'slug' => $slug['slug'],
'type' => $type,
'recipients' => $slug['recipient'],
'updated_at' => $lastUpdated,
];
});


}

public static function table(Table $table)
{
return $table
->query(function () {
$collection = self::getTableQuery();

$currentPage = request()->input('page', 1);
$perPage = 15;

// Paginate the collection
$paginatedCollection = new LengthAwarePaginator(
$collection->forPage($currentPage, $perPage), // Items for the current page
$collection->count(), // Total items
$perPage,
$currentPage,
[
'path' => request()->url(), // Base URL for pagination links
'query' => request()->query(), // Retain query parameters
]
);

return $paginatedCollection;
})

->columns([
TextColumn::make('label')
->label('Label')
->sortable()
->searchable(),

TextColumn::make('slug')
->label('Slug')
->sortable()
->searchable(),

TextColumn::make('type')
->label('Type')
->sortable(),

TextColumn::make('recipients')
->label('Recipients')
->sortable(),

TextColumn::make('updated_at')
->label('Last Updated')
->dateTime()
->sortable(),
]);
}
public static function getTableQuery()
{

$instance = new self();
$notifications = collect($instance->getNotificationSlugs());

$slugs = [];
foreach ($notifications as $notification) {
if (isset($notification['sms'])) {
$slugs[] = $notification['sms'];
}

if (isset($notification['email'])) {
$slugs[] = $notification['email'];
}
}

$options = Option::query()
->whereIn('slug', $slugs)
->get()
->pluck('updated_at', 'slug')
->toArray();

return $notifications->map(function ($slug) use ($options) {
$hasSms = isset($slug['sms']);
$hasEmail = isset($slug['email']);

$type = match(true) {
$hasSms && $hasEmail => 'Email, SMS',
$hasSms => 'SMS',
$hasEmail => 'Email',
};

$lastUpdated = match(true) {
$hasEmail => data_get($options, $slug['email']),
$hasSms => data_get($options, $slug['sms']),
default => null,
};

return [
'label' => $slug['label'],
'slug' => $slug['slug'],
'type' => $type,
'recipients' => $slug['recipient'],
'updated_at' => $lastUpdated,
];
});


}

public static function table(Table $table)
{
return $table
->query(function () {
$collection = self::getTableQuery();

$currentPage = request()->input('page', 1);
$perPage = 15;

// Paginate the collection
$paginatedCollection = new LengthAwarePaginator(
$collection->forPage($currentPage, $perPage), // Items for the current page
$collection->count(), // Total items
$perPage,
$currentPage,
[
'path' => request()->url(), // Base URL for pagination links
'query' => request()->query(), // Retain query parameters
]
);

return $paginatedCollection;
})

->columns([
TextColumn::make('label')
->label('Label')
->sortable()
->searchable(),

TextColumn::make('slug')
->label('Slug')
->sortable()
->searchable(),

TextColumn::make('type')
->label('Type')
->sortable(),

TextColumn::make('recipients')
->label('Recipients')
->sortable(),

TextColumn::make('updated_at')
->label('Last Updated')
->dateTime()
->sortable(),
]);
}
Everything works inside the query at ->query(function () {}) already used dd() on everyline but where did this error come from ? Method Illuminate\Support\Collection::clone does not exist. I didn't used clone() but its causing the error
23 Replies
Dennis Koch
Dennis Koch6d ago
You return a PaginatedCollection from the ->query() method which should return a Builder instance.
o.m
o.mOP6d ago
LengthAwarePaginator() should convert it back to builder right from collection? Even doing thisreturns the same issue
return $notifications
->map(function ($slug) use ($options) {
(....)



public function table(Table $table): Table
{
$collection = $this->getTableData();

return $table
->query(fn() => $collection);
}
return $notifications
->map(function ($slug) use ($options) {
(....)



public function table(Table $table): Table
{
$collection = $this->getTableData();

return $table
->query(fn() => $collection);
}
Wait... LengthAwarePaginator is not a builder instance? Is it possible to converted PaginatedCollection to builder instance?
Dennis Koch
Dennis Koch6d ago
No, it's a paginator instance. No it's not possible to covert a paginator to a database query builder 😅
o.m
o.mOP6d ago
Im doomed..
Dennis Koch
Dennis Koch6d ago
Yes, because collection also isn't a builder 😅
o.m
o.mOP6d ago
Does table only accept Builder instance?
Dennis Koch
Dennis Koch6d ago
Why do you use a Pagintor? Filament handles pagination Yes. It only works with Eloquent.
o.m
o.mOP6d ago
Do you have any suggestion for me ?
Dennis Koch
Dennis Koch6d ago
I have no idea what you are trying to do with that paginator?
o.m
o.mOP6d ago
The paginator is just there because I am trying to migrate a code to filament and it was used I actually removed it in the current setup
o.m
o.mOP6d ago
No description
o.m
o.mOP6d ago
toQuery didnt work Hmm what should I do
Dennis Koch
Dennis Koch6d ago
Filament v3 does not support static data. You can try using the Sushi package. I don't have much experience with that, but it was answered many times here on Discord. Filament v4 will allow static data.
Dennis Koch
Dennis Koch6d ago
Yes. Both are the same. One is a blog post 😅
o.m
o.mOP6d ago
Sorry how do I make custom view? in a custom page
Dennis Koch
Dennis Koch6d ago
Create a view file and set it via the $view property. But that should be the default for custom pages.
o.m
o.mOP6d ago
Hi, i get this error when using LengAwarePaginator()
public function mount()
{
$this->notifications = $this->getTableData();
}

public function getTableData() // returns LengthAwarePaginator()
{
public function mount()
{
$this->notifications = $this->getTableData();
}

public function getTableData() // returns LengthAwarePaginator()
{
o.m
o.mOP6d ago
No description
o.m
o.mOP6d ago
Do you have any other way to pass the variable into the view ?
o.m
o.mOP6d ago
After converthing everything to this
No description
o.m
o.mOP6d ago
I still get this error Property type not supported in Livewire for property: [{"current_page":1,"data":{"11":{"label":"Add User Group","slug":"add_user_group","type":"Email","recipients":"UNotification","slug":"rem ........}] Is there no render() in filament so I could do this?
public function render()
{
$collection = $this->getTableData();
$paginator = new LengthAwarePaginator(
$collection->forPage($this->page, $this->perPage === 'all' ? $collection->count() : $this->perPage),
$collection->count(),
$this->perPage === 'all' ? $collection->count() : $this->perPage,
$this->page
);

return view('livewire.datatable.notifications', [
'notifications' => $paginator,
]);
}
public function render()
{
$collection = $this->getTableData();
$paginator = new LengthAwarePaginator(
$collection->forPage($this->page, $this->perPage === 'all' ? $collection->count() : $this->perPage),
$collection->count(),
$this->perPage === 'all' ? $collection->count() : $this->perPage,
$this->page
);

return view('livewire.datatable.notifications', [
'notifications' => $paginator,
]);
}
Dennis Koch
Dennis Koch6d ago
Filament is based on Livewire, so there is render() on the pages which are Livewire components

Did you find this page helpful?