Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

FARDEEN's avatar

API not returning updated data

I have this weird problem. It works fine on my local machine. But the problem happens when I upload this project on the live server (in a sub-domain for testing purposes). GitHub Repository

The project has one Model (other than User), "Post"

The project has one route and one API endpoint. Initially, I should get the same results from both the web route and API endpoint. The problem is when I delete a post from the web, the API result still shows that deleted post. The API returns updated results after 20-30 minutes.

Very strange issue.

If anyone knows anything about such an issue, please help me out.

I am using Livewire

App\Http\Livewire\Posts.php

public function render()
    {
        $posts = DB::table('posts')
        ->select(DB::raw("
            posts.id AS id,
            posts.title AS title,
            posts.category AS category,
            posts.author AS author
        "))
        ->orderBy('posts.category')
        ->get();

        $data = [
            'posts' => $posts
        ];

        return view('livewire.posts', $data);
    }

api.php

Route::get('posts', function() {
    $posts = DB::table('posts')
        ->select(DB::raw("
            posts.id AS id,
            posts.title AS title,
            posts.category AS category,
            posts.author AS author
        "))
        ->orderBy('posts.category');

    return response()->json($posts->get(), 200);
});
0 likes
17 replies
tykus's avatar

How/where are you testing this?

FARDEEN's avatar

@tykus Both on my server ($5 Linode) and local machine. Testing this in a sub-domain

tykus's avatar
tykus
Best Answer
Level 104

@FARDEEN any database clustering, or automatic caching?

FARDEEN's avatar

@tykus I use MariaDB. Basically, I installed myvestacp, which includes PHP, Apache, Nginx, MariaDB, and other basic things. And the PHP has Zend OPcache.

I have tried

php artisan cache:clear
php artisan optimize:clear 

before using API. But not effect

tykus's avatar

@FARDEEN on the face of it, the same query is being executed in both the render method of the Livewire component and the API Route closure; there should be no difference.

Where do you test the API response - browser, Postman, Insomnia?

tykus's avatar

@FARDEEN can you try to set the Cache-Control: no-cache header in Postman to see if we can isolate where the problem might lie.

FARDEEN's avatar

@tykus I Just tried after setting "Send no-cache header" ON. But no change

tykus's avatar

@FARDEEN and for clarity, while the API is returning the original data (including the deleted Post), the Livewire page is correctly displaying the updated collection of Posts; even after reloading???

tykus's avatar

@FARDEEN Is there a transaction that is not committed??? Is the database actually being queried on the API requests??? I'm reaching now...

FARDEEN's avatar

@tykus Actually, it was my fault. I did try

php artisan cache:clear
php artisan optimize:clear

And it did not work since there was a caching configuration in Nginx.

You know, when you are a noob at server management and end up using a nice and ready solution like myvestacp, you may end up making silly mistakes like this.

The way myvestacp is configured, it utilizes a combination of Apache and Nginx (as proxy). Without knowing the outcome, I had set the Nginx proxy as "caching" instead of "hosting-public". This is why the API result would get cached and only update after some time.

I did not face any issues so far because I did not use API before.

But thank you anyway for your time and consultation. Appreciate it.

2 likes
Lumethys's avatar

@FARDEEN good to know you had resolved your issue, but your query is exactly like

$posts = Post::get();

why are you even you raw db query?

FARDEEN's avatar

@Lumethys Maybe it is a personal preference. I write my own SQL queries, whether it is simple or complex, and always try to optimize them.

And I found Query Builder more pleasing when it comes to converting SQL to PHP. On top of that, I have once read that Query Builder can be faster for larger datasets. Did not benchmark it myself, though.

Sinnbeck's avatar

@FARDEEN Correct. Query builder is faster than eloquent as it does not need to hydrate an eloquent model (casts and such). But you can still use eloquent syntax (no with() though) and just convert it to query builder

Post::query()
    ->toBase() //convert to query builder
    ->get();
1 like
fytrah's avatar

Hi @fardeen did you find a solution to this problem? I got the same issue, API not returning latest data. It takes some time to get latest data when hitting API.

FARDEEN's avatar

@fytrah It was due to caching. Nginx was set to a "caching" template. This was the problem. I have explained in detail, in a reply above. Just use Ctrl+F/Cmd+F and search this term "@tykus Actually, it was my fault. I did try"

you will find my explanation.

Please or to participate in this conversation.