Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid calling query() twice by adding a local var, fixes #4133 and #4140 #4221

Open
wants to merge 5 commits into
base: 3.1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/Sheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -465,13 +465,18 @@ public function fromView(FromView $sheetExport, $sheetIndex = null)
*/
public function fromQuery(FromQuery $sheetExport, Worksheet $worksheet)
{
if ($sheetExport->query() instanceof \Laravel\Scout\Builder) {
$query = $sheetExport->query();
if ($query instanceof \Laravel\Scout\Builder) {
$this->fromScout($sheetExport, $worksheet);

return;
}

$sheetExport->query()->chunk($this->getChunkSize($sheetExport), function ($chunk) use ($sheetExport) {
//Operate on a clone to avoid altering the original
//and use the clone operator directly to support old versions of Laravel
//that don't have a clone method in eloquent
$clonedQuery = clone $query;
$clonedQuery->chunk($this->getChunkSize($sheetExport), function ($chunk) use ($sheetExport) {
$this->appendRows($chunk, $sheetExport);
});
}
Expand Down
18 changes: 18 additions & 0 deletions tests/Concerns/FromQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Maatwebsite\Excel\Tests\Data\Stubs\FromUsersQueryExport;
use Maatwebsite\Excel\Tests\Data\Stubs\FromUsersQueryExportWithEagerLoad;
use Maatwebsite\Excel\Tests\Data\Stubs\FromUsersQueryExportWithPrepareRows;
use Maatwebsite\Excel\Tests\Data\Stubs\FromUsersQueryWithJoinExport;
use Maatwebsite\Excel\Tests\Data\Stubs\FromUsersScoutExport;
use Maatwebsite\Excel\Tests\TestCase;

Expand Down Expand Up @@ -61,6 +62,23 @@ public function test_can_export_from_query()
$this->assertEquals($allUsers, $contents);
}

public function test_can_export_from_query_with_join()
{
$export = new FromUsersQueryWithJoinExport();

$response = $export->store('from-query-store.xlsx');

$this->assertTrue($response);

$contents = $this->readAsArray(__DIR__ . '/../Data/Disks/Local/from-query-store.xlsx', 'Xlsx');

$allUsers = $export->query->get()->map(function (User $user) {
return array_values($user->toArray());
})->toArray();

$this->assertEquals($allUsers, $contents);
}

public function test_can_export_from_relation_query_queued()
{
$export = new FromGroupUsersQueuedQueryExport();
Expand Down
46 changes: 46 additions & 0 deletions tests/Data/Stubs/FromUsersQueryWithJoinExport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Maatwebsite\Excel\Tests\Data\Stubs;

use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Database\Query\Builder;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\WithCustomChunkSize;
use Maatwebsite\Excel\Tests\Data\Stubs\Database\User;

class FromUsersQueryWithJoinExport implements FromQuery, WithCustomChunkSize
{
use Exportable;

public $query;

public function __construct()
{
$this->query = User::query();
}

/**
* @return Builder|EloquentBuilder|Relation
*/
public function query()
{
return $this->query
->join(
'group_user',
'group_user.user_id',
'=',
'users.id'
)
->select('users.*', 'group_user.group_id as gid');
}

/**
* @return int
*/
public function chunkSize(): int
{
return 10;
}
}
Loading