Last active
January 25, 2016 16:52
-
-
Save Ardakilic/6352909 to your computer and use it in GitHub Desktop.
To find all unbanned/unsuspended users with Sentry 2 using Laravel 4, try this method:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class User extends Cartalyst\Sentry\Users\Eloquent\User { | |
/** | |
* @author Arda Kılıçdağı, | |
* @param $orderBy string how to order | |
* @param $sortBy string Sort like Ascending or Descending, values are asc/desc | |
* @param $toArray boolean whether to return the output as array or not. Default is false | |
* | |
* Access it like: User::activeUsers(); | |
* To alter sorting, e.g: to get latest users first: User::activeUsers('user_id', 'desc'); | |
* To get output like array, pass the 3rd parameter as true: User::activeUsers(null, null, true); | |
**/ | |
public static function activeUsers($orderBy='first_name', $sortBy='asc', $toArray=false) { | |
$out = Sentry::getUserProvider()->createModel() | |
->join('throttle', 'throttle.user_id', '=', 'users.id', 'left') | |
->where(function($q){ | |
$q->where('throttle.banned', 0); | |
$q->orWhere('throttle.banned', null); | |
}) | |
->where(function($q){ | |
$q->where('throttle.suspended', 0); | |
$q->orWhere('throttle.suspended', null); | |
}) | |
->groupBy('users.id') | |
->orderBy('users.'.$orderBy, $sortBy) | |
->select('users.*') | |
->get(); | |
if($toArray) { | |
$out = $out->toArray(); | |
} | |
return $out; | |
} | |
} |
Nice! But what if I want to perform a search (username LIKE seachTerm) to get a subset of the active users? I tried where clauses everywhere, but to no avail. Can you help me?
@macliems - Have you tried using something like where('users.first_name', 'LIKE', '%pattern%');
?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated the method a bit. I've put it in a User model which extends Sentry 2 Eloquent User model. You can change your app/models/User.php with this if you want to use Sentry 2 instead Laravel 4's auth methods.