Support the ongoing development of Laravel.io →
Database Eloquent

I have a scope that looks like this

public function scopeNotDefault() {
    return $this->where('pp_id', '!=', 1);
}

If I use that scope in a query, it forces the result to ALWAYS include ALL columns.

For example, this... results in retrieving the entire table (all columns)

$pics = ppModel::select('pp_id', 'pp_number')->notDefault()->get()

But if I instead do NOT use the scope. Like this.....

$pics = ppModel::select('pp_id', 'pp_number')->where('pp_id', '!=', 1)->get()

Then that will return only the two columns specified.

Is this supposed to happen? What's the point of using select and scopes at the same time if so? The select does nothing when using scopes.

Am I using scopes wrong?

Last updated 3 years ago.
0

Yes you are using scopes wrong. Scopes receive the current query builder and return the builder. You are creating a new query builder. If you look at the docs you will see all the examples for local scopes receive a $query variable and call methods on that $query variable.

public function scopeNotDefault($query) {
    return $query->where('pp_id', '!=', 1);
}
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Nertskull nertskull Joined 3 Apr 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.