Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static function filter($request)
- {
- $columns = $request->get('columns');
- $order = $request->get('order');
- $customers = self::select([
- 'customers.id AS id',
- 'customers.name AS name',
- 'customers.phone AS phone',
- 'customers.email AS email',
- 'customers.address AS address',
- 'customers.website AS website',
- 'customers.comment AS comment',
- 'customers.created_at AS created_at',
- 'cs.name AS status',
- 'ct.name AS type',
- 'c.name AS city',
- 'm.surname AS manager_surname',
- 'm.name AS manager_name',
- 'm.fathername AS manager_fathername'
- ])
- ->leftJoin('customers_statuses AS cs', function($q){
- $q->on(DB::raw('customers.status_id = cs.id'), DB::raw(''), DB::raw(''));
- })
- ->leftJoin('customers_types AS ct', function($q){
- $q->on(DB::raw('customers.type_id = ct.id'), DB::raw(''), DB::raw(''));
- })
- ->leftJoin('cities AS c', function($q){
- $q->on(DB::raw('customers.city_id = c.id'), DB::raw(''), DB::raw(''));
- })
- ->leftJoin('users AS m', function($q) {
- $q->on('customers.manager_id', '=', 'm.id');
- });
- if($columns){
- foreach($columns as $column){
- if(isset($column['search']['value']) && !empty($column['search']['value']) && $column['searchable'] == 'true'){
- if(preg_match('#_at#', $column['name'])) {
- $parts = explode('-', $column['search']['value']);
- if (isset($parts[0]) && !empty($parts[0]) && isset($parts[1]) && !empty($parts[1])) {
- $customers->whereBetween('customers.' . $column['name'], [(new Carbon($parts[0]))->format('Y-m-d'), (new Carbon($parts[1]))->format('Y-m-d')]);
- } else if (isset($parts[0]) && !empty($parts[0])) {
- $customers->where('customers.' . $column['name'], '>', (new Carbon($parts[0]))->format('Y-m-d'));
- } else if (isset($parts[1]) && !empty($parts[1])) {
- $customers->where('customers.' . $column['name'], '<', (new Carbon($parts[1]))->format('Y-m-d'));
- }
- }else if($column['name'] == 'name') {
- $customers->where(function ($q) use ($column) {
- $q->where('customers.name', 'LIKE', $column['search']['value'] . '%');
- });
- }else if($column['name'] == 'id'){
- $customers->where('customers.' . $column['name'], '=', $column['search']['value']);
- }else{
- $customers->where($column['name'], '=', $column['search']['value']);
- }
- }
- }
- }
- if(isset($columns[$order[0]['column']]['data']) && isset($order[0]['dir'])){
- if($columns[$order[0]['column']]['orderable'] == 'false'){
- $columns[$order[0]['column']]['data'] = 'id';
- }
- $customers->orderBy($columns[$order[0]['column']]['data'], $order[0]['dir']);
- }
- return $customers;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement