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

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

Closed
1 task done
Max-Hutschenreiter opened this issue May 17, 2024 · 4 comments
Closed
1 task done

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

Max-Hutschenreiter opened this issue May 17, 2024 · 4 comments

Comments

@Max-Hutschenreiter
Copy link

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

  • 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.

@sinnbeck
Copy link

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");
}`

@camohub
Copy link

camohub commented Jun 3, 2024

How this issue end up?
I have similar but worse issue. I need to use queue and this destroys my query.
#4140

Copy link

stale bot commented Aug 10, 2024

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.

@Synchro
Copy link

Synchro commented Oct 16, 2024

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 added a commit to Synchro/Laravel-Excel that referenced this issue Oct 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants