A
A
Alexander2016-11-07 16:04:18
Laravel
Alexander, 2016-11-07 16:04:18

Is this a normal code? and how it can be optimized (data filter from database)?

I came across this hefty code:

Long code

public static function filter($visa_type, $status, $company)
    {
        if (Auth::user()->hasRole('administrator|Manager')) {
            if ($visa_type && $status & $company) {
                return self::where('visa_type', $visa_type)
                    ->where('status', $status)
                    ->where('company', $company)
                    ->orderBy('id', 'desc')
                    ->paginate(40)
                    ->toArray()['data'];
            } else if ($visa_type && $status) {
                return self::where('visa_type', $visa_type)
                    ->where('status', $status)
                    ->orderBy('id', 'desc')
                    ->paginate(40)
                    ->toArray()['data'];
            } else if ($visa_type && $company) {
                return self::where('visa_type', $visa_type)
                    ->where('status', $status)
                    ->orderBy('id', 'desc')
                    ->paginate(40)
                    ->toArray()['data'];
            } else if ($status && $company) {
                return self::where('company', $company)
                    ->where('status', $status)
                    ->orderBy('id', 'desc')
                    ->paginate(40)
                    ->toArray()['data'];
            } else if ($visa_type) {
                return self::where('visa_type', $visa_type)
                    ->orderBy('id', 'desc')
                    ->paginate(40)
                    ->toArray()['data'];
            } else if ($status) {
                return self::where('status', $status)
                    ->orderBy('id', 'desc')
                    ->paginate(40)
                    ->toArray()['data'];
            } else {
                return self::where('company', $company)
                    ->orderBy('id', 'desc')
                    ->paginate(40)
                    ->toArray()['data'];
            }
        } else {
            if ($visa_type && $status & $company) {
                return self::where('user_id', Auth::user()->id)
                    ->where('visa_type', $visa_type)
                    ->where('status', $status)
                    ->where('company', $company)
                    ->orderBy('id', 'desc')
                    ->paginate(40)
                    ->toArray()['data'];
            } else if ($visa_type && $status) {
                return self::where('user_id', Auth::user()->id)
                    ->where('visa_type', $visa_type)
                    ->where('status', $status)
                    ->orderBy('id', 'desc')
                    ->paginate(40)
                    ->toArray()['data'];
            } else if ($visa_type && $company) {
                return self::where('user_id', Auth::user()->id)
                    ->where('visa_type', $visa_type)
                    ->where('status', $status)
                    ->orderBy('id', 'desc')
                    ->paginate(40)
                    ->toArray()['data'];
            } else if ($status && $company) {
                return self::where('user_id', Auth::user()->id)
                    ->where('company', $company)
                    ->where('status', $status)
                    ->orderBy('id', 'desc')
                    ->paginate(40)
                    ->toArray()['data'];
            } else if ($visa_type) {
                return self::where('user_id', Auth::user()->id)
                    ->where('visa_type', $visa_type)
                    ->orderBy('id', 'desc')
                    ->paginate(40)
                    ->toArray()['data'];
            } else if ($status) {
                return self::where('user_id', Auth::user()->id)
                    ->where('status', $status)
                    ->orderBy('id', 'desc')
                    ->paginate(40)
                    ->toArray()['data'];
            } else {
                return self::where('user_id', Auth::user()->id)
                    ->where('company', $company)
                    ->orderBy('id', 'desc')
                    ->paginate(40)
                    ->toArray()['data'];
            }
        }
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrzej Wielski, 2016-11-07
@kentuck1213

More rubbery, using collections and scope functions:

public function scopeFilter($query, $parameters = []){
    $fields = ['visa_type', 'status', 'company'];
    $filter = collect([
      'visa_type' => null,
      'status' => null,
      'company' => null,
      'paginate' => 40,
      'order' => 'id',
      'sort' => 'desc'
    ])->merge($parameters);

    if (!Auth::user()->hasRole('administrator|Manager')) {
      $query->where('user_id', Auth::user()->id);
    }
    
    foreach($filter->only($fields) as $field => $value){
      $query->where($field, $value);
    }

    return $query
      ->orderBy($filter->get('order'), $filter->get('sort'))
      ->paginate($filter->get('paginate'))
      ->toArray()['data'];
}

Usage -
SomeModel::where(...)->filter([
   'visa_type' => '....',
   'paginate' => 20,
   'sort' => 'asc'
]);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question