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

MartiX's avatar

Can't retrieve all records from the database with Eloquent or DB

Hi guys,

I am facing a strange problem and I can't figure out what's wrong. I have a database with posts table which contains more than 13 000 rows. When I manually try to make an SQL query and select some records, everything works fine.

Now when I start the server and try to make a query I can't get all the records. To be specific, I've just got 10 922 records from 13 317. Where are those 2K records?

This is not a regular database I've made with Laravel, it's Wordpress and I am also using Corcel package, but it's really strange that I can't get all the records. It seems like something is blocking the query to run over all of the rows, but even when I tried to delete some records after the record 10 922, nothing has changed. The records are there but I just can't get them.

    Post::all();
    Post::status('publish')->get();
    DB::table('posts')->get();

I tried it with pagination, orderBy different columns, limit and everything else I know when it comes to querying the DB.

I cleared cache, restart the server and everything else, but the result is same. Are there other procedures that I've forgot to do? Thanks in advance.

0 likes
23 replies
Dalma's avatar

I don't think that the DB::table query would use global scopes or not but have tried something like this on the eloquent queries?

// Remove all of the global scopes...
Post::withoutGlobalScopes()->get();

In case you have soft deletes configured?

hanif-king's avatar

getting 13 000 records in a single query is not a good practice try to chuck each section of the query result and display it . for example see this.

DB::table('posts')->orderBy('id')->chunk(100, function ($post) {
    foreach ($post as $postData) {
        // do your stuff here with 100's of record and you can increase the numbers too.
    }
});

i hope you it could help you in your problem .

MartiX's avatar

Dalma I tried it as well, but still the latest post is 4 months old. What do you mean by soft deletes? In fact, I did not directly configured any models, they comes from Corcel package. But thanks, I will try some other ways with scopes.

hanif-king, sorry but it seems like you didn't even read my post, I don't have problem to chunk the results or limit/paginate the collection, my problem is, that I just can't get the very latest records from the database. The latest records I get are 4 months old, that's the problem.

Cronix's avatar

This will order them by the latest records using the created_at field in the db table. If you are using a different datatime field, you can specify it

Post::latest()->get(); 
// select * from `posts` order by `created_at` desc

or

Post::latest('timestamp_field')->get();
// select * from `posts` order by `timestamp_field` desc
MartiX's avatar

Cronix thanks, but this is fine, I can get all the post or latest posts by any parameter, the problem is, that the database somehow block the query to run over all records (rows) and I can't get all of them.

The latest posts I can get are from 05.01.2018. Anything above this date is just ignored, even though the records are clearly in the database and when I manually write an SQL query in sequel pro, everything works.

Cronix's avatar

What is the query you're running in sequel pro? Just "select * from posts"?

MartiX's avatar

Yup. I tried select * from posts, then select * from posts where ... (parameters) and everything worked. But after couple of hours I just can't get those records in my app.

Cronix's avatar

Very strange indeed. The only time I've had something like that happen is when I was working with 2 databases that weren't in sync. The live database had all records, while the local database didn't, and I was viewing the data in the live database in navicat/sequel pro/whatever.

MartiX's avatar

That may be the case, because I am also working with 2 databases, but I'm using the only one of them - the wordpress database. The other one is just a basic dabatase after fresh laravel installation and migration.

One thing which is really strange is, that at 05.01.2018 I've made some changes to the Wordpress project (change the theme, made some custom posts..) but I just don't see the connection, where's the problem. I tried all possible parameters, but still can't get those records after 05.01.2018.

MartiX's avatar

jlrdw, yes I tried to run raw SQL query as well and the result was the same. I've got exactly 10 922 records, but in reality there's 13 292 records.

MartiX's avatar

I tried to make some debugging with enableQueryLog and here's an interesting result, which maybe someone could understand.

DB::connection('wordpress')->enableQueryLog();

        $post = Post::find(id);

        dd(DB::connection('wordpress')->getQueryLog());

This is the code I've used. Now when I put an id of the post, which was posted before the 05.01.2018, I've got a result. Two arrays.

0 => array:3 [▼
    "query" => "select * from `debncpposts` where `debncpposts`.`ID` = ? limit 1"
    "bindings" => array:1 [▶]
    "time" => 4.34
  ]
  1 => array:3 [▼
    "query" => "select * from `debncppostmeta` where `post_id` in (?)"
    "bindings" => array:1 [▶]
    "time" => 1.49
  ]

When I make a query with an id, which belongs to the post from 06.01. or more recent, I've got just one array.

0 => array:3 [▼
    "query" => "select * from `debncpposts` where `debncpposts`.`ID` = ? limit 1"
    "bindings" => array:1 [▶]
    "time" => 3.71
  ]

Any ideas, please?

jlrdw's avatar

Have you tried running a query on phpmyadmin or something to see what you get there. Have you verified all records are in that table.

MartiX's avatar

Yes jlrdw, I've used sequel pro (just cleaner and nicer than ugly phpmyadmin). It's all working and the records are clearly there. I can get them with raw SQL commands, just can't get them in my app.

Cronix's avatar

@MartiX I'd start looking at this "Corcel" package you mentioned. I have a feeling this problem is coming from that.

Instead of

Post::all();

try

DB::connection('wordpress')->table('posts')->get();

and see if there is a difference

MartiX's avatar

Cronix this returns the same result as Post::all() in this case too. Just the collection of 10 922 records (more than 2000 still missing). Now I made my own Model and tried to find an ID from the database for the recent post, but I've got null.

I think Corcel is okay, because I have the same results with my own models or DB (even raw SQL queries don't return all the records).

It seems like something is blocking it. Maybe some DB bug, corrupted row? I don't really know.

Cronix's avatar

Is there anything in the error logs? I don't think there is a bug in DB or the forum would be filled with posts similar to this, and this is the first time I recall seeing this sort of problem.

MartiX's avatar

Not at all. I've read almost all the log file and did not find anything related to this sort of problem. This is the most frustrating experience I've had in last months.

Cronix's avatar

Understandable. The last thing I'd suggest is to set up a new laravel instance. Don't include any packages and just create a Post model and see if that makes any difference. Other than that, without having your code and database to play with, I don't really have any other suggestions.

Is 'wordpress' your default database connection?

Do you have a posts table in both databases, or just the wordpress database?

MartiX's avatar

I've already made that, but I'll try to do it again without package as you said and see the results. Maybe I'll try to clear all the tables and re-import the whole DB.

I think it's not default, but in every model there's protected $connection with 'wordpress'.

And posts table is only in the wordpress database, the other one is almost empty (there are just migrations and users table).

MartiX's avatar
MartiX
OP
Best Answer
Level 3

Finally! I've got it working and really don't know how.

I wipe out all the tables, then made a backup of live DB and made a big cleanup (delete all revisions, empty rows and so on (wordpress database is really a junk full of craps after time). Then I've just imported this let's say less uglier database and it's working. Oh, now I can go rest with peace in my mind.

Thanks everyone for participating and time, especially you @Cronix

Cronix's avatar

@MartiX You're welcome and glad you got it resolved! Please mark the thread as solved.

1 like

Please or to participate in this conversation.