Laravel-Excel icon indicating copy to clipboard operation
Laravel-Excel copied to clipboard

[Bug]: Export FromQuery double join statements in query.

Open Max-Hutschenreiter opened this issue 1 year ago • 2 comments
trafficstars

Is the bug applicable and reproducable to the latest version of the package and hasn't it been reported before?

  • [X] Yes, it's still reproducable

What version of Laravel Excel are you using?

3.1.55

What version of Laravel are you using?

v10.48.7

What version of PHP are you using?

8.3.7

Describe your issue

I wrote a class implementing FromQuery and With Headings to use Exportable.

I used the __constructor to pass a Query from outside, which I am referencing in the query method. This is working fine. At the moment, when I add a join to the query method, it adds it twice. If I am not passing the query over the constructor, it's working. If I am adding the joins outside and than pass it its also working.

Call of the class $query = Review::query(); return (new ExcelReviewExport($query))->download('reviews.xlsx');

`class ExcelReviewExport implements FromQuery, WithHeadings { use Exportable;

public $query;

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

public function query(){
    return $this->query
    ->select(['id'])
    ->join("products", "reviews.product_id", '=', "products.id");
}`

This is not stated in the documentation and maybe was never supposed to be used like this, but it would be nice if it worked. I will add the joins as a workaround outside of the class, so this is really low prior for me.

Thanks for the great work on this Package!

How can the issue be reproduced?

Create an ExcelExport Class with FromQuery. Pass the query over to the constructor. Add a join in the query method to the passed query.

What should be the expected behaviour?

Adding joins only once.

Max-Hutschenreiter avatar May 17 '24 09:05 Max-Hutschenreiter

I would expect that it is running the query() twice and as laravels query builder is using classes, it will add the join twice as well.

So try doing a clone

 public function query(){
    return $this->query
    ->clone()
    ->select(['id'])
    ->join("products", "reviews.product_id", '=', "products.id");
}`

sinnbeck avatar May 31 '24 14:05 sinnbeck

How this issue end up? I have similar but worse issue. I need to use queue and this destroys my query. https://github.com/SpartnerNL/Laravel-Excel/issues/4140

camohub avatar Jun 03 '24 08:06 camohub

This bug report has been automatically closed because it has not had recent activity. If this is still an active bug, please comment to reopen. Thank you for your contributions.

stale[bot] avatar Aug 10 '24 13:08 stale[bot]

This is definitely a bug (also reported in #4140), but I managed to work around it by adding a check to see whether query() had been called before, thus avoiding the duplicate joins. More info in here, but the short version is to add this at the top of your query method:

static $done = false;
if ($done) {
    return $this->query;
}
$done = true;
//Now carry on making your modifications to `$query`

Synchro avatar Oct 16 '24 15:10 Synchro