Consume an External API With Filament Tables

I have an existing resource , model and migration table in my project and now I am trying to add an external api to the model so that the table will show the api data not the migration table data ok , but it gives me an driver not found error so what should I do I use this plugin https://filamentphp.com/community/how-to-consume-an-external-api-with-filament-tables
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.
13 Replies
LeandroFerreira
LeandroFerreira16mo ago
https://github.com/calebporzio/sushi check the sushi instructions
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.
AliBabba420
AliBabba420OP16mo ago
No bro it's not working, it gives me could not find driver error and ask for creating the table but i give the api? All the data that needed in the resources column is available in the api .
Vp
Vp16mo ago
Can you post your actual error, for me it's working perfectly
AliBabba420
AliBabba420OP16mo ago
Ok so i have a resource , model and migration table also working perfectly and showing the data in the table But i wnt to get the data from external api so i create an api in another project which is running loacally in my system And now I am trying to call that api in my existing model and shoe that api data in my existing resource table but it gives me driver not find error and told me to create the migration table
krekas
krekas16mo ago
POST THE ACTUAL ERROR AND CODE WHAT YOU ARE DOING
AliBabba420
AliBabba420OP16mo ago
No description
No description
AliBabba420
AliBabba420OP16mo ago
No description
AliBabba420
AliBabba420OP16mo ago
the local api controller <?php namespace App\Http\Controllers; use App\Models\Contact; use Illuminate\Http\Request; class ContactController extends Controller { public function contacts(Request $request) { $batchSize = 50; //dd($batchSize); $page = $request->query('page', 1); //dd($page); $offset = ($page - 1) * $batchSize; //dd($offset); $data = Contact::skip($offset) ->take($batchSize) ->get(); //dd($data[0]); return response()->json([ 'contacts' => $data ]); } }
AliBabba420
AliBabba420OP16mo ago
No description
AliBabba420
AliBabba420OP16mo ago
ok sry i just use an external api for the model and now it gives me this error
No description
cheesegrits
cheesegrits16mo ago
An array to string conversation from Sushi is usually where one (or more) of the attributes from your API source are arrays. I've never managed to get Sushi to cast arrays to json itself (using $schema) when creating the internal sqlite table, I have to manually json_encode any array attributes in getRows(). You can $cast them to array so they get turned back in to arrays when accessing those attributes. But seems like you have to encode them yourself. So dd a row of your api data, and see if any of the attribute data are arrays. So for example, in this model, the 'mask' attribute from the JSON I'm reading is an array, so I have to map it to json when feeding it to Sushi in getRows().
class Country extends Model
{
use Sushi;

public $incrementing = false;

protected $keyType = 'string';

protected $primaryKey = 'iso';

protected $casts = [
'mask' => 'array',
];

protected $schema = [
'name' => 'string',
'code' => 'string',
'iso' => 'string',
'flag' => 'string',
'mask' => 'json',
];

public function getRows(): array
{
$rows = array_map(
function (array $row) {
$row['mask'] = json_encode(Arr::wrap($row['mask']));

return $row;
},
File::json(__DIR__ . '/countries.json')
);

return $rows;
}

protected function sushiShouldCache(): bool
{
return false;
}

protected function sushiCacheReferencePath(): string
{
return __DIR__ . '/countries.json';
}
}
class Country extends Model
{
use Sushi;

public $incrementing = false;

protected $keyType = 'string';

protected $primaryKey = 'iso';

protected $casts = [
'mask' => 'array',
];

protected $schema = [
'name' => 'string',
'code' => 'string',
'iso' => 'string',
'flag' => 'string',
'mask' => 'json',
];

public function getRows(): array
{
$rows = array_map(
function (array $row) {
$row['mask'] = json_encode(Arr::wrap($row['mask']));

return $row;
},
File::json(__DIR__ . '/countries.json')
);

return $rows;
}

protected function sushiShouldCache(): bool
{
return false;
}

protected function sushiCacheReferencePath(): string
{
return __DIR__ . '/countries.json';
}
}
AliBabba420
AliBabba420OP16mo ago
Ok so it's just the api has array of object and inside the there are object inside object but when I remove those part it works perfectly One more thing can we make an resource who get the data from external api and show in the table but while create some data it store in the migration table ?
cheesegrits
cheesegrits16mo ago
No, this wouldn't be possible. The Filament resource is built on a model. Your model is aa Sushi model, so Sushi overrides all database handling. It seems to me like Sushi is the wrong approach for this, if you need to be able to store local changes to the data. I would suggest having a normal local model / table, then a scheduled queue job to synchronize between your local table and the API data. Store the API data primary key in a separate field, like api_key (so don't use it as the main PK for your model/migration). In your scheduled job, read the API data, and upsert into your local table (against api_key) as required. If you need to feed changes back to the API, do that as well.
Want results from more Discord servers?
Add your server