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

[6.x] Allow to use scoped macro in nested queries #30127

Merged
merged 2 commits into from Oct 3, 2019

Conversation

@dkulyk
Copy link
Contributor

dkulyk commented Sep 28, 2019

Sometimes need to use scope macro in nested queries:

Model::query()->where(function (Builder $query) {
    $query->withoutTrashed()
        ->orWhereDate('created_at', '>', Carbon::now()->subMonth());
})->withTrashed();

//Real example:
Access::query()
    ->where('type', 'free')
    ->orWhere(function (Builder $query) {
        $query
            ->where('type', 'protected')
            ->forUser(Auth::user()); //scoped macro with nested logic
    })

Scopes are applying only when you call get() or cursor(). But In nested queries this not happening.

I believe this is bug what scope macro does not work in nested queries.

@GrahamCampbell GrahamCampbell changed the title Allow to use scoped macro in nested queries. [6.x] Allow to use scoped macro in nested queries. Sep 28, 2019
@staudenmeir

This comment has been minimized.

Copy link
Contributor

staudenmeir commented Sep 28, 2019

We can't use newQuery(), it adds $withCount and its bindings to the nested query (#24240).

@dkulyk

This comment has been minimized.

Copy link
Contributor Author

dkulyk commented Sep 28, 2019

I'll explore this. But where are test for it?

@dkulyk

This comment has been minimized.

Copy link
Contributor Author

dkulyk commented Sep 28, 2019

newQueryWithoutRelationships does what is needed.

@staudenmeir

This comment has been minimized.

Copy link
Contributor

staudenmeir commented Sep 28, 2019

Sorry, you can use newQuery(). I forgot the binding issue has also been fixed (and tested) in #24000.

@GrahamCampbell GrahamCampbell changed the title [6.x] Allow to use scoped macro in nested queries. [6.x] Allow to use scoped macro in nested queries Sep 28, 2019
@dkulyk

This comment has been minimized.

Copy link
Contributor Author

dkulyk commented Sep 28, 2019

Sorry, you can use newQuery(). I forgot the binding issue has also been fixed (and tested) in #24000.

Cannot. I catch Error: Maximum function nesting level of '256' reached, aborting! if Model has non empty $withCount

@derekmd

This comment has been minimized.

Copy link
Contributor

derekmd commented Sep 28, 2019

256 is xdebug's default stack depth limit in local development. Disabling the PHP extension will also make the test suite run much faster.

@GrahamCampbell

This comment has been minimized.

Copy link
Member

GrahamCampbell commented Sep 29, 2019

@derekmd The most likely reason one would encounter that error is if there was infinite recursion in the call. It's unlikely that the stack depth limit was hit, otherwise. Disabling the extension would likely see php segfault instead.

@taylorotwell taylorotwell merged commit 88adf85 into laravel:6.x Oct 3, 2019
2 checks passed
2 checks passed
continuous-integration/styleci/pr The analysis has passed
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
@dkulyk dkulyk deleted the dkulyk:scope_macro_in_nested_query branch Oct 3, 2019
Gman98ish added a commit to Gman98ish/framework that referenced this pull request Oct 4, 2019
Remove brackets arround URL php artisan serve (laravel#30168)

To allow opening the development URL in Visual Studio Code with ctrl + click the bracket after the URL needs to be removed.
This patch wil remove the brackets around the url.

add test for sorted middlewares (laravel#30166)

[6.x] Apply limit to database rather than collection (laravel#30148)

* Apply limit to database rather than collection

For HasInDatabase.php

* Fix tests

* Add to SoftDeleted trait as well

* Update HasInDatabase.php

* Update SoftDeletedInDatabase.php

[6.x] Allow to use scoped macro in nested queries (laravel#30127)

* Allow to use scoped macro in nested queries.

* Use newQueryWithoutRelationships
Gman98ish added a commit to Gman98ish/framework that referenced this pull request Oct 4, 2019
Remove brackets arround URL php artisan serve (laravel#30168)

To allow opening the development URL in Visual Studio Code with ctrl + click the bracket after the URL needs to be removed.
This patch wil remove the brackets around the url.

add test for sorted middlewares (laravel#30166)

[6.x] Apply limit to database rather than collection (laravel#30148)

* Apply limit to database rather than collection

For HasInDatabase.php

* Fix tests

* Add to SoftDeleted trait as well

* Update HasInDatabase.php

* Update SoftDeletedInDatabase.php

[6.x] Allow to use scoped macro in nested queries (laravel#30127)

* Allow to use scoped macro in nested queries.

* Use newQueryWithoutRelationships
i-bajrai added a commit to i-bajrai/framework that referenced this pull request Oct 4, 2019
* Allow to use scoped macro in nested queries.

* Use newQueryWithoutRelationships
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants
You can’t perform that action at this time.