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

query scopes for models #12

Closed
hardevine opened this issue May 13, 2013 · 6 comments
Closed

query scopes for models #12

hardevine opened this issue May 13, 2013 · 6 comments

Comments

@hardevine
Copy link

is it possible to have autocompletion query scopes methods while using on model eg.
User::active()->get();

@barryvdh
Copy link
Owner

I think you can create a @method phpdoc in your class for the method active and return static (if using netbeans/phpstorm) or the model itself (or just Eloquent)
It is not in the model helper yet, but I think I will implement it soon.

@hardevine
Copy link
Author

yeah, static works great..
eloquent or model was not working as return type,
..though could not get other chained methods eg. get, toArray etc.
User::active()->get()
thanks..

 *
 *  @method static active() get active users
 *
 */
class User extends Eloquent {

barryvdh added a commit that referenced this issue May 20, 2013
@barryvdh
Copy link
Owner

Should be detected automatically now..

@lsalazarm99
Copy link

It's only working when the scope is the very first method you use when you are building the query.

If you need to use 2 scopes or something like the following (assuming an "active" scope), it's not working.

$models = YourModel::query()
    ->when(
        $request->boolean('isActive', false),
        function (Builder $query) {
            $query->active(); // Warning: Method 'active' not found in \Illuminate\Database\Eloquent\Builder
        }
    )
    ->get()
;

@dragg
Copy link

dragg commented May 13, 2021

@lsalazarm99
I don't see any reason why it should works on your example but I assume if you change to some kind of the next example then it should define fine:

$models = YourModel::query()
    ->when(
        $request->boolean('isActive', false),
        /** @var Builder|YourModel $query */
        function (Builder $query) {
            $query->active(); // Warning: Method 'active' not found in \Illuminate\Database\Eloquent\Builder
        }
    )
    ->get()

@nandi95
Copy link

nandi95 commented Aug 12, 2024

@barryvdh Is it possible to have this a a member function on eloquent?

MyModel::where('thing', 1)->myScope()->paginate()

I suspect it isn't so straightforward as it should only be applicable for a specific model.

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

No branches or pull requests

5 participants