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
Merged

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

merged 2 commits into from
Oct 3, 2019

Conversation

dkulyk
Copy link
Contributor

@dkulyk 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
Copy link
Contributor

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

@dkulyk
Copy link
Contributor Author

dkulyk commented Sep 28, 2019

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

@dkulyk
Copy link
Contributor Author

dkulyk commented Sep 28, 2019

newQueryWithoutRelationships does what is needed.

@staudenmeir
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
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
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
Copy link
Member

@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
@dkulyk dkulyk deleted the scope_macro_in_nested_query branch October 3, 2019 16:35
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 pushed 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
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants