#Sushi to dynamically load data from API using search keyword

7 messages · Page 1 of 1 (latest)

ocean token
#

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.

<?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);
    }
}

Thank you

silver coyoteBOT
#

To help others find answers, you can mark your question as solved via Right click solution message -> Apps -> ✅ Mark Solution

ocean token
#

This is my Model code

<?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 [];
    }
}

Thank you

#

keywords are basically just numeric input

ocean token
#

anyone please ?

sullen glacier
#

did you have any luck with this?

ocean token
#

Hi @sullen glacier yes, I used request()->keyword in getRows() function and also set sushiShouldCache to false.