How to get stock market values in Filament without saving to database?

51 Views Asked by At

I just started to learn how to use Filament. I understand that it uses the values in our database to display them in a table format. However, here's the challenge. I want to create a website to track the current prices of the stock market and take some actions based on them. Due to the fluctuating stock prices, I can't save the price value to my database. I am considering using DOMCrawler. Do you have any suggestions? Thanks for the help in advance.

This is the code I wrote.

public static function table(Table $table): Table
{
    $stocks = Stock::orderBy('id','ASC')->get();

    $prices = [];
    $daily_percentage = [];
    foreach ($stocks as $stock) {
        $client = new Client();
        $res = $client->request('GET', "Stock_Market_URL");
        $body = $res->getBody()->getContents();

        $crawler = new Crawler($body);

        $prices[] = $crawler->filter('.detR .realTime')->children()
            ->eq(0)->text();

        $crawler = new Crawler($body);

        $daily_percentage[] = $crawler->filter('.detR .realTime table tr')->children()
            ->eq(1)->filter('span')->eq(0)->text();
    }

    return $table
        ->columns([
            Tables\Columns\TextColumn::make('name')
                ->searchable(),
            Tables\Columns\TextColumn::make('price'),
            Tables\Columns\TextColumn::make('rate_of_change_daily'),
        ])
        ->filters([
            //
        ])
        ->actions([
            Tables\Actions\EditAction::make(),
        ])
        ->bulkActions([
            Tables\Actions\BulkActionGroup::make([
                Tables\Actions\DeleteBulkAction::make(),
            ]),
        ]);
}

I can crawl and retrieve the prices, but I'm struggling to display them in the table because I don't know how to do it.

0

There are 0 best solutions below