Laravel-excel: [BUG] globalscopes are not applied on export

Created on 25 Jun 2020  路  19Comments  路  Source: Maatwebsite/Laravel-Excel

Prerequisites

  • [x] Checked if your Laravel Excel version is still supported: https://docs.laravel-excel.com/3.1/getting-started/support.html#supported-versions
  • [x] Able to reproduce the behaviour outside of your code, the problem is isolated to Laravel Excel.
  • [x] Checked that your issue isn't already filed.
  • [x] Checked if no PR was submitted that fixes this problem.
  • [x] Filled in the entire issue template

Versions

  • PHP version: PHP 7.4.7 (cli) (built: Jun 12 2020 00:00:24) ( NTS )
  • Laravel version: Laravel Framework 7.17.2
  • Package version: 3.1.19

Description

package doesn't respect the global-scopes on export ex.

protected static function booted()
    {
        static::addGlobalScope('latest_items', function (Builder $builder) {
            $builder->latest(
                $builder->qualifyColumn('created_at')
            );
        });
    }

query is correct ex.

select * from `requests` order by `requests`.`created_at` desc  

but the exported sheet is not sorted.

Steps to Reproduce

  • add data with different dates
  • add globalscope to ordeby
  • export the sheet via query

Expected behavior:

  • globalscopes should be respected.

Actual behavior:

  • sheet is not sorted according to globalscope.
bug

All 19 comments

I would need to see a bit more code, I'm not sure what "static" points to and I can't see what kind of query you use.

the query is simple as Request::query() and the static is a global scope under the model, example updated

Can you try putting that global scope in a service provider, I'm not sure the booted callbacks gets called due to performance reasons.

actually booted is the correct place https://laravel.com/docs/master/eloquent#global-scopes

I'm not saying it isn't. I'm trying to find out why it's not being called.

yeah i understand, np i will give it a try.

note that using through a service provider will make it take longer for the fw to resolve the class and its binding b4 using it.

sorry for the late reply, tried the service provider but sadly gave the same result.

Are you using from query and should queue?

using FromQuery yes & no queuing

i believe the issue is with the export function not the query, because i can get a correct sql.

It seems Laravel's chunk/chunkById methods don't respect the global scopes. They work when I specify the orderBy on the query itself.

You could try to reproduce without this package and see:

$first = Request::query()->first();
Request::query()->chunk(100, function($chunk) use($first) {

// Compare first results
dd($first, $chunk->first());
});

Not sure if bug in Laravel or if it's intended to work like this.

i've tried to search if others had the same issue, only one ticket was made but then closed at the same day without any further details.

maybe we can try to use cursors instead ? which is better than chunking for large data sets.

That will be the default in 3.2 yes.

In 3.1 you can return the cursor in the collection method

so this bug should be fixed in next version u mean ?

Yes it should work out of the box in the next version

any estimation when will that be ?

Sorry, I don't have any ETA for it.

public function collection()
{
    return Request::query()->cursor()
}

^ this should already be enough for you I think

i dont think this would work with the query setup, still thanx again, if you want u can close this ticket.

It would replace the query() method your have right now. Give it a go!

Was this page helpful?
0 / 5 - 0 ratings