Advertisement
Guest User

Untitled

a guest
Sep 4th, 2015
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. public static function filter($request)
  2. {
  3.  
  4. $columns = $request->get('columns');
  5. $order = $request->get('order');
  6.  
  7. $customers = self::select([
  8. 'customers.id AS id',
  9. 'customers.name AS name',
  10. 'customers.phone AS phone',
  11. 'customers.email AS email',
  12. 'customers.address AS address',
  13. 'customers.website AS website',
  14. 'customers.comment AS comment',
  15. 'customers.created_at AS created_at',
  16. 'cs.name AS status',
  17. 'ct.name AS type',
  18. 'c.name AS city',
  19. 'm.surname AS manager_surname',
  20. 'm.name AS manager_name',
  21. 'm.fathername AS manager_fathername'
  22. ])
  23. ->leftJoin('customers_statuses AS cs', function($q){
  24. $q->on(DB::raw('customers.status_id = cs.id'), DB::raw(''), DB::raw(''));
  25. })
  26. ->leftJoin('customers_types AS ct', function($q){
  27. $q->on(DB::raw('customers.type_id = ct.id'), DB::raw(''), DB::raw(''));
  28. })
  29. ->leftJoin('cities AS c', function($q){
  30. $q->on(DB::raw('customers.city_id = c.id'), DB::raw(''), DB::raw(''));
  31. })
  32. ->leftJoin('users AS m', function($q) {
  33. $q->on('customers.manager_id', '=', 'm.id');
  34. });
  35.  
  36. if($columns){
  37. foreach($columns as $column){
  38. if(isset($column['search']['value']) && !empty($column['search']['value']) && $column['searchable'] == 'true'){
  39. if(preg_match('#_at#', $column['name'])) {
  40. $parts = explode('-', $column['search']['value']);
  41. if (isset($parts[0]) && !empty($parts[0]) && isset($parts[1]) && !empty($parts[1])) {
  42. $customers->whereBetween('customers.' . $column['name'], [(new Carbon($parts[0]))->format('Y-m-d'), (new Carbon($parts[1]))->format('Y-m-d')]);
  43. } else if (isset($parts[0]) && !empty($parts[0])) {
  44. $customers->where('customers.' . $column['name'], '>', (new Carbon($parts[0]))->format('Y-m-d'));
  45. } else if (isset($parts[1]) && !empty($parts[1])) {
  46. $customers->where('customers.' . $column['name'], '<', (new Carbon($parts[1]))->format('Y-m-d'));
  47. }
  48. }else if($column['name'] == 'name') {
  49. $customers->where(function ($q) use ($column) {
  50. $q->where('customers.name', 'LIKE', $column['search']['value'] . '%');
  51. });
  52. }else if($column['name'] == 'id'){
  53. $customers->where('customers.' . $column['name'], '=', $column['search']['value']);
  54. }else{
  55. $customers->where($column['name'], '=', $column['search']['value']);
  56. }
  57. }
  58. }
  59. }
  60.  
  61. if(isset($columns[$order[0]['column']]['data']) && isset($order[0]['dir'])){
  62. if($columns[$order[0]['column']]['orderable'] == 'false'){
  63. $columns[$order[0]['column']]['data'] = 'id';
  64. }
  65.  
  66. $customers->orderBy($columns[$order[0]['column']]['data'], $order[0]['dir']);
  67. }
  68.  
  69. return $customers;
  70.  
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement