Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a query scope in a collection laravel

My Association model looks like this (irrelevant code redacted):

class Association extends Model
{
    public function members() {
        return $this->hasMany('App\Member');
    }

}

My Member model looks like this:

class Member extends Model
{
    public function scopeActive($query) {
        return $query->where('membership_ended_at', Null);
    }
    public function scopeInactive($query) {
        return $query->whereNotNull('membership_ended_at');
    }
}

This is what I want to be able to do:

$association = Association::find(49);
$association->members->active()->count();

Now, I'm aware there's a difference between a Query and a Collection. But what I'm basically asking is if there's some kind of similar scope for collections. Of course, the optimal solution would be to not have to write TWO active methods, but use one for both purposes.

like image 500
Pistachio Avatar asked Jun 19 '15 13:06

Pistachio


People also ask

What is scope query in Laravel?

Laravel Scope. Scoping is one of the superpowers that eloquent grants to developers when querying a model. Scopes allow developers to add constraints to queries for a given model. Utilizing Eloquent's Scoping helps us to follow the DRY principles when writing queries for our models.

What is Take () in Laravel?

take() will help to get data from a database table with a limit. skip() will help to skip some records when you fetch data from the database table. So, let's see bellow examples that will help you how to use take() and skip() eloquent query in laravel.

What is eager loading in Laravel?

Eager loading is super simple using Laravel and basically prevents you from encountering the N+1 problem with your data. This problem is caused by making N+1 queries to the database, where N is the number of items being fetched from the database.

How can use array in query in Laravel?

If you need to use sql wherein query in laravel then you can use with array. Laravel provide wherein() to use sql wherein query. in wherein() we just need to pass two argument one is column name and another if array of ids or anything that you want.

When to use Laravel query scopes?

When you work with small and large laravel web application. At that time you need to get/fetch active records from the database tables using the laravel query scopes. Laravel query scopes to save your time to write code again and again.

How to get/fetch active records from the database tables in Laravel?

At that time you need to get/fetch active records from the database tables using the laravel query scopes. Laravel query scopes to save your time to write code again and again.

How does soft delete work in Laravel?

Laravel's own soft delete functionality utilizes global scopes to only pull "non-deleted" Eloquent models from the database. Writing your own global scopes can provide a convenient, easy way to make sure every query for a given model receives certain constraints.

What is the scope of a query?

A scope is just a convenience method you can add to your model which encapsulates the syntax used to execute a query such as the above. Scopes are defined by prefixing the name of a method with scope, as demonstrated here: With the scope defined, you may call the scope methods when querying the model like so:


1 Answers

(question already answered in the comments, but might as well write a proper answer)

It is not possible to use a query scope in a Colletion, since query scope is a concept used in Eloquent to add constraints to a database query while Collections are just a collection of things (data, objects, etc).

In your case, what you need to do is to change this line:

$association->members->active()->count();

to:

$association->members()->active()->count();

This works because when we call members as a method, we are getting a QueryBuilder instance, and with that we can start chaining scopes to the query before calling the count method.

like image 70
Artenes Nogueira Avatar answered Sep 28 '22 08:09

Artenes Nogueira