Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@dillinghamio
Last active June 18, 2016 17:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dillinghamio/a1af87f56a5c46a17c4dfdb0247e081e to your computer and use it in GitHub Desktop.
Save dillinghamio/a1af87f56a5c46a17c4dfdb0247e081e to your computer and use it in GitHub Desktop.

Laravel Model Scope Array

Use array syntax to define a list of scopes to apply to a model

Assuming you have scopePopular & scopeRecent as query scopes

Book::scopes(['popular', 'recent'])->get();

Add this to your model

public function scopeScopes($query, $scopes)
{
        foreach($scopes as $scope)
        {
            $query->$scope();
        }

        return $query;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment