Can we use collection or array in table query?

I am creating a custom table in Filament, and I want to use a collection of data in the table. However, it seems that the table query in Filament does not support collections. What should I do to work with collections in my custom table? Error Filament\Tables\Table::query(): Argument #1 ($query) must be of type Illuminate\Database\Eloquent\Builder|Closure|null, Illuminate\Support\Collection given, called in /var/www/html/app/Filament/Pages/Reports/AccountStatement.php on line 45 Code
public function table(Table $table): Table
{


$record = Account::where('account_type', Account::BANK)->first();


return $table
->query($record->transactionsQuery(Carbon::yesterday() ,Carbon::tomorrow())->get() )
->columns([
TextColumn::make('name')
])
->filters([
// ...
])
->actions([
// ...
])
->bulkActions([
// ...
]);
}
public function table(Table $table): Table
{


$record = Account::where('account_type', Account::BANK)->first();


return $table
->query($record->transactionsQuery(Carbon::yesterday() ,Carbon::tomorrow())->get() )
->columns([
TextColumn::make('name')
])
->filters([
// ...
])
->actions([
// ...
])
->bulkActions([
// ...
]);
}
Collection Data
Illuminate\Support\Collection {#2175 ▼ // app/Filament/Pages/Reports/AccountStatement.php:43
#items: array:4 [▼
0 => {#2205 ▼
+"id": 1
+"transaction_date": "2023-09-16 10:54:41"
+"transaction_no": "CP01/0001"
+"reference": null
+"transaction_type": "CP"
+"credited": 1
+"narration": "Rent "
+"rate": "1.0000"
}
1 => {#2168 ▼
+"id": 2
+"transaction_date": "2023-09-16 11:55:13"
+"transaction_no": "CP01/0002"
+"reference": null
+"transaction_type": "CP"
+"credited": 1
+"narration": "Consectetur "
+"rate": "1.0000"
}
]
#escapeWhenCastingToString: false
}
Illuminate\Support\Collection {#2175 ▼ // app/Filament/Pages/Reports/AccountStatement.php:43
#items: array:4 [▼
0 => {#2205 ▼
+"id": 1
+"transaction_date": "2023-09-16 10:54:41"
+"transaction_no": "CP01/0001"
+"reference": null
+"transaction_type": "CP"
+"credited": 1
+"narration": "Rent "
+"rate": "1.0000"
}
1 => {#2168 ▼
+"id": 2
+"transaction_date": "2023-09-16 11:55:13"
+"transaction_no": "CP01/0002"
+"reference": null
+"transaction_type": "CP"
+"credited": 1
+"narration": "Consectetur "
+"rate": "1.0000"
}
]
#escapeWhenCastingToString: false
}
9 Replies
awcodes
awcodes10mo ago
You can if you use sushi. There’s an article about it. Don’t have the link at the moment. Just do a search I’m sure you find it.
waleedGRT
waleedGRT10mo ago
GitHub
GitHub - calebporzio/sushi: Eloquent's missing "array" driver.
Eloquent's missing "array" driver. Contribute to calebporzio/sushi development by creating an account on GitHub.
awcodes
awcodes10mo ago
Yea that. But there’s an actual article about how to implement it with Filament tables.
waleedGRT
waleedGRT10mo ago
I believe you are referring to this particular package. Let me verify if it suits my needs or not.
waleedGRT
waleedGRT10mo ago
Filament
How to consume an external API with Filament Tables by Leandro Ferr...
A collection of beautiful full-stack components for Laravel. The perfect starting point for your next app. Using Livewire, Alpine.js and Tailwind CSS.
awcodes
awcodes10mo ago
That’s the one. But ultimately tables has to be an eloquent query. I know there’s some work going on around making it possible to use arrays etc, but there’s a lot of caveats and edge cases to being able to do it reliably.
waleedGRT
waleedGRT10mo ago
Just wanted to drop a quick note to say thanks for recommending the sushi package. It did the trick and solved my problem perfectly. Your suggestion was spot on, and I really appreciate it!
awcodes
awcodes10mo ago
No worries. Glad you got it working.
waleedGRT
waleedGRT10mo ago
I apologize for bothering you, but I'd like to know how to pass dynamic values to a controller. I've successfully created a controller using Sushi, and now I'm looking to pass a dynamic value to the getRows method of Sushi from the Filament Resource class. Controller
<?php

namespace App\Models;

use Reports\AccountStatement as ReportsAccountStatement;
use Illuminate\Database\Eloquent\Model;
use Sushi\Sushi;

class AccountStatement extends Model
{
use Sushi;

/**
* Model Rows
*
* @return void
*/
public function getRows()
{

$accountStatement = new ReportsAccountStatement( ACCOUNT_NO , START_DATE , END_DATE );

$accountStatement = (array) $accountStatement->getTransactions();

return json_decode(json_encode($accountStatement), true);
}
}
<?php

namespace App\Models;

use Reports\AccountStatement as ReportsAccountStatement;
use Illuminate\Database\Eloquent\Model;
use Sushi\Sushi;

class AccountStatement extends Model
{
use Sushi;

/**
* Model Rows
*
* @return void
*/
public function getRows()
{

$accountStatement = new ReportsAccountStatement( ACCOUNT_NO , START_DATE , END_DATE );

$accountStatement = (array) $accountStatement->getTransactions();

return json_decode(json_encode($accountStatement), true);
}
}
Somehow I managed to solve the setting parameter dynamically but I again hint at another problem of filtering the record based on account Controller
<?php
namespace App\Models;

use Reports\AccountStatement as ReportsAccountStatement;
use Illuminate\Database\Eloquent\Model;
use Sushi\Sushi;

class AccountStatement extends Model
{
use Sushi;

public static $accountId;
public static $startDate;
public static $EndDate;

public static function setParameters($accountId, $startDate = null, $EndDate = null)
{
self::$accountId = $accountId;
self::$startDate = $startDate;
self::$EndDate = $EndDate;
return self::query();
}
public function getRows()
{
if (self::$accountId) {
$accountStatement = new ReportsAccountStatement(self::$accountId);
$accountStatement = (array) $accountStatement->getTransactions();
return json_decode(json_encode($accountStatement), true);
} else {
return [];
}
}
}
<?php
namespace App\Models;

use Reports\AccountStatement as ReportsAccountStatement;
use Illuminate\Database\Eloquent\Model;
use Sushi\Sushi;

class AccountStatement extends Model
{
use Sushi;

public static $accountId;
public static $startDate;
public static $EndDate;

public static function setParameters($accountId, $startDate = null, $EndDate = null)
{
self::$accountId = $accountId;
self::$startDate = $startDate;
self::$EndDate = $EndDate;
return self::query();
}
public function getRows()
{
if (self::$accountId) {
$accountStatement = new ReportsAccountStatement(self::$accountId);
$accountStatement = (array) $accountStatement->getTransactions();
return json_decode(json_encode($accountStatement), true);
} else {
return [];
}
}
}
Filament Resource
public static function table(Table $table): Table
{
AccountStatement::setParameters(3); // setting default account

return $table
->query(AccountStatement::query())
->columns(...)
->filters([
SelectFilter::make('account_id')
->options(Account::query()->pluck('name', 'id')->toArray())
->searchable(true)->columnSpan(2)
->preload()
->baseQuery(function (Builder $query, array $data): Builder {

AccountStatement::setParameters($data['value']); // THIS SETTING VALUE NOT WORKING!

return $query;
}),

], layout: FiltersLayout::AboveContent);
}
public static function table(Table $table): Table
{
AccountStatement::setParameters(3); // setting default account

return $table
->query(AccountStatement::query())
->columns(...)
->filters([
SelectFilter::make('account_id')
->options(Account::query()->pluck('name', 'id')->toArray())
->searchable(true)->columnSpan(2)
->preload()
->baseQuery(function (Builder $query, array $data): Builder {

AccountStatement::setParameters($data['value']); // THIS SETTING VALUE NOT WORKING!

return $query;
}),

], layout: FiltersLayout::AboveContent);
}
How can we update the static value of the model based on the filter? ⬆️ Can someone tell me how we can interact with table query?