Skip to content

Instantly share code, notes, and snippets.

@Ardakilic
Last active January 25, 2016 16:52
Show Gist options
  • Save Ardakilic/6352909 to your computer and use it in GitHub Desktop.
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:
<?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;
}
}
@Ardakilic
Copy link
Author

@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