skoobi's avatar
Level 13

Eloquent only returning 1 item from ->get()

Hi.

I'm coming accross a strange issue where im grabing columns from the DB but only 1 is being returned. It seems to be happening with a few of the queries im doing where it was working fin but not anymore!

Heres one query to grab scans from the database (there are 2 items which are almost identical but different dates)

$collection = CustomerScan::where('user_id', Auth::id())->groupBy('created_at')->orderBy('created_at', 'desc')->get()->groupBy(function ($item) {
            return $item->created_at->format('Y-m-d');
        });

If i dump that $collection to the screen it only shows 1 item instead of 2.

The other one is routhly the same

$scan_all_demands = User::role('Customer')
        ->findMailCentre($mailcentre)
        ->hasScanAllDemand()
        ->accountIsActive()
        ->with(['scans'])
        ->get();

// In the model from a hasMany relationship

// scan all demand
    public function scopeHasScanAllDemand($query)
    {
        return $query->whereHas('scans', function ($query) {
            $query->where('scan_all', 1);
        });
    }

this returns only the first item which 'scan_all' is set to '0'!

Is there something im missing!!!

Any help would be greatful.

Many thanks

0 likes
5 replies
skoobi's avatar
Level 13

Ok...

I found on the code below, that if i want to grab all the columns and they have the exact same date/time then it will only get the first item due to being grouped by created_at.

$collection = CustomerScan::where('user_id', Auth::id())->groupBy('created_at')->orderBy('created_at', 'desc')->get()->groupBy(function ($item) {
            return $item->created_at->format('Y-m-d');
        });

If im uploading an image and it creates the database entry in under a second for the two items then the created_at will be the exact same.

Any ideas on how i can group them by date/time without hitting this conflict?

Im still working on the other issue..

Many thanks

Sinnbeck's avatar

If you dont want them to be grouped in the query, just dont :)

$collection = CustomerScan::where('user_id', Auth::id())->orderBy('created_at', 'desc')->get()->groupBy(function ($item) {
            return $item->created_at->format('Y-m-d');
        });
skoobi's avatar
Level 13

Hi @sinnbeck, thanks for your reply.

Unfortunetly the customer wants them to be displayed by day on the template.

A temporary workaround for now is to add a sleep(1) when the images are uploaded so that theres a second gap between the columns. Theres only upto 5 small images (under 50kb each) being uploaded at any one time and its only the admin who use this, so for the 5 seconds wait, it should be ok. Just thought it was an odd issue.

ftiersch's avatar

You can just get all of them and then group them later in the collection.

For example like this:

$data = CustomerScan::get()->groupBy(function ($scan) {
    return $scan->created_at->toDateString();
});

(of course you will have to add all your where methods and stuff, just remove the group by from the query and move it to the end :))

Edit: I just noticed you did that already. So yes, @sinnbeck is right. Just remove the groupBy from the query (not the last one) and you should get your result.

Sinnbeck's avatar

But you already group them by day after the query. No need to group them twice. Please try my suggestion. It should work :)

Please or to participate in this conversation.