Sushi to dynamically load data from API using search keyword
Hello,
I am using Sushi to load a data into a table with API data using a search keyword.
But I am missing something here as I am not able to load the data from API after the search but if I provide the search term initially then it loads the proper data to the table.
Please check my code and help me fix this, thank you so much.
This is my custom page code, removed the imports due to limitations.
Thank you
<?php
namespace App\Filament\Dashboard\Pages;
class TargetFinderGenerator extends Page implements HasForms, HasTable
{
use InteractsWithTable, InteractsWithForms;
public $keyword;
public $selected = [];
protected static ?string $navigationIcon = 'heroicon-o-arrow-right-end-on-rectangle';
protected static ?string $navigationGroup = 'Target Finder';
protected static string $view = 'filament.dashboard.pages.target-finder-generator';
public ?array $data = [];
public function form(Form $form): Form
{
return $form
->statePath('data')
->schema([
Grid::make(2)
->schema([
TextInput::make("keyword")
->default(2)
->required(),
])
]);
}
public static function table(Table $table): Table
{
return $table
->columns([
TextColumn::make("name"),
TextColumn::make("email"),
TextColumn::make("body"),
])
->filters([])
->actions([])
->bulkActions([])
->query(TargetFinder::query());
}
public function fetchApiData()
{
$this->selected = [];
(new TargetFinder)->getRows($this->data);
}
}
<?php
namespace App\Filament\Dashboard\Pages;
class TargetFinderGenerator extends Page implements HasForms, HasTable
{
use InteractsWithTable, InteractsWithForms;
public $keyword;
public $selected = [];
protected static ?string $navigationIcon = 'heroicon-o-arrow-right-end-on-rectangle';
protected static ?string $navigationGroup = 'Target Finder';
protected static string $view = 'filament.dashboard.pages.target-finder-generator';
public ?array $data = [];
public function form(Form $form): Form
{
return $form
->statePath('data')
->schema([
Grid::make(2)
->schema([
TextInput::make("keyword")
->default(2)
->required(),
])
]);
}
public static function table(Table $table): Table
{
return $table
->columns([
TextColumn::make("name"),
TextColumn::make("email"),
TextColumn::make("body"),
])
->filters([])
->actions([])
->bulkActions([])
->query(TargetFinder::query());
}
public function fetchApiData()
{
$this->selected = [];
(new TargetFinder)->getRows($this->data);
}
}
1 Reply
This is my Model code
Thank you
keywords are basically just numeric input
anyone please ?
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Http;
class TargetFinder extends Model
{
use \Sushi\Sushi;
public function getRows($data = [])
{
// Fetch data from your API
if (count($data)) {
$response = Http::get('https://jsonplaceholder.typicode.com/posts/' . $data['keyword'] . '/comments');
return $response->json();
} else {
$response = Http::get('https://jsonplaceholder.typicode.com/posts/1/comments');
return $response->json();
}
return [];
}
}
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Http;
class TargetFinder extends Model
{
use \Sushi\Sushi;
public function getRows($data = [])
{
// Fetch data from your API
if (count($data)) {
$response = Http::get('https://jsonplaceholder.typicode.com/posts/' . $data['keyword'] . '/comments');
return $response->json();
} else {
$response = Http::get('https://jsonplaceholder.typicode.com/posts/1/comments');
return $response->json();
}
return [];
}
}