0

I have two method's in the User class that return different queries. I would like to use both like this

$users = User::fromCountry('DE')
                    ->isMaster()
                    ->paginate(20);

but it is not working, since isMaster() is a method from User and not from the query builder:

Call to undefined method Illuminate\Database\Query\Builder::isMaster()

I also tried

$users = User::fromCountry('DE')
                    ->(User::isMaster())
                    ->paginate(20);

but this is also not working. Is it possible to easily combine my queries from the User class?


Just in case its necessary, here are the two methods from the User class:

public static function isMaster()
    {
      switch (orga) {
        case 1:
          return static::where('modeIN','=','MT');

        case 2:
          return static::where('modeICI','=','CMT');

        case 3:
          return static::where('modeWHO','=','HMT');
      }
    }


    public static function fromCountry($country)
    {
      return static::where(
              function($query) use($country){
                $query->whereHas('addresses',
                          function($q) use($country){
                            $q->where('idCountry','=',$country);
                          }
                        )
                      ->orWhereHas('institutes',
                        function($q) use($country){
                          $q->whereHas('addresses',
                            function($q) use($country){
                              $q->where('idCountry','=',$country);
                            });
                        }
                      );
             });
    }

3 Answers 3

1

Try changing your modal function to:

public function scopeIsMaster($query)
    {
      switch (orga) {
        case 1:
          return $query->where('modeIN','=','MT');

        case 2:
          return $query->where('modeICI','=','CMT');

        case 3:
          return $query->where('modeWHO','=','HMT');
      }
    }

public function scopeFromCountry($query,$country)
    {
      return $query->where(
              function($query) use($country){
                $query->whereHas('addresses',
                          function($q) use($country){
                            $q->where('idCountry','=',$country);
                          }
                        )
                      ->orWhereHas('institutes',
                        function($q) use($country){
                          $q->whereHas('addresses',
                            function($q) use($country){
                              $q->where('idCountry','=',$country);
                            });
                        }
                      );
             });
    }
Sign up to request clarification or add additional context in comments.

2 Comments

DId you not make it static on purpose? Do I understand it right that I have to split my query call into two calls: $users = User::fromCountry('DE'); and $users = User::queryIsMaster($users)->paginate(20); ?
no you just call $users = User::fromCountry('DE') ->isMaster() ->paginate(20);
1

This is what you can do. Laravel uses scope for stuff like this

User {

   public function scopeOfType($query, $orga)
    {
        switch ($orga) {
        case 1:
          return $query->where('modeIN','=','MT');

        case 2:
          return $query->where('modeICI','=','CMT');

        case 3:
          return $query->where('modeWHO','=','HMT');
      }
    }


}

Then call it like this.

User::ofType(1);
User::ofType(2);
User::ofType(3);

For more info check https://laravel.com/docs/5.3/eloquent#local-scopes

Comments

0

Have a look at Laravel Scope :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.