mysql - Laravel 4 Method Improvement -
i have index method:
public function index() { // in view, there several multiselect boxes (account managers, company names , account types). code retrives values post method of form/session. $company_names_value = input::get('company_names_value'); $account_managers_value = input::get('account_managers_value'); $account_types_value = input::get('account_types_value'); // if there has been no form submission, check if values empty , if assign default. // essentially, of records in table column required. if (is_null($company_names_value)) { $company_names_value = db::table('accounts') ->orderby('company_name') ->lists('company_name'); } if (is_null($account_managers_value)) { $account_managers_value = db::table('users') ->orderby(db::raw('concat(first_name," ",last_name)')) ->select(db::raw('concat(first_name," ",last_name) amname')) ->lists('amname'); } if (is_null($account_types_value)) { $account_types_value = db::table('account_types') ->orderby('type') ->lists('type'); } // in view, there dropdown box, allows user select amount of records show per page. retrieve value or set default. $perpage = input::get('perpage', 10); // code retrieves order session has been selected user clicking on table column title. value placed in session via getorder() method , used later in eloquent query , joins. $order = session::get('account.order', 'company_name.asc'); $order = explode('.', $order); // here perform joins required , order records, select accounts , select id's aid. wherein used select records company name, account manager name , account type matches values of multiselect boxes or default set above. $accounts_query = account::leftjoin('users', 'users.id', '=', 'accounts.user_id') ->leftjoin('account_types', 'account_types.id', '=', 'accounts.account_type_id') ->orderby($order[0], $order[1]) ->select(array('accounts.*', db::raw('accounts.id aid'))); if (!empty($company_names_value)) $accounts_query = $accounts_query->wherein('accounts.company_name', $company_names_value); $accounts = $accounts_query->wherein(db::raw('concat(users.first_name," ",users.last_name)'), $account_managers_value) ->wherein('account_types.type', $account_types_value) ->paginate($perpage)->appends(array('company_names_value' => input::get('company_names_value'), 'account_managers_value' => input::get('account_managers_value'), 'account_types_value' => input::get('account_types_value'))); $accounts_trash = account::onlytrashed() ->leftjoin('users', 'users.id', '=', 'accounts.user_id') ->leftjoin('account_types', 'account_types.id', '=', 'accounts.account_type_id') ->orderby($order[0], $order[1]) ->select(array('accounts.*', db::raw('accounts.id aid'))) ->get(); $message = session::get('message'); $default = ($perpage === null ? 10 : $perpage); $this->layout->content = view::make('admin.accounts.index', array( 'accounts' => $accounts, 'accounts_trash' => $accounts_trash, 'company_names' => db::table('accounts')->orderby('company_name')->lists('company_name', 'company_name'), 'account_managers' => db::table('users')->orderby(db::raw('concat(first_name," ",last_name)'))->select(db::raw('concat(first_name," ",last_name) amname'))->lists('amname', 'amname'), 'account_types' => db::table('account_types')->orderby('type')->lists('type', 'type'), 'perpage' => $perpage, 'message' => $message, 'default' => $default )); }
basically, building query searches several tables (hence joins). in view user has ability select multiple values various multi-select boxes , submit form populate $company_names_value, $account_managers_value , $account_types_value variables.
initially, when there no form submission, i'm using query builder select records each type, , using them in query.
it works slow , messy. wondering if of laravel 4 gurus me improve further, queries faster , code lighter.
thanks in advance.
this has been refactored significantly, , fast now. i've moved of code in models, refactoring code.
here's new index method:
public function index() { $account = explode(',', input::get('account')); $account_manager = explode(',', input::get('account_manager')); $account_type = explode(',', input::get('account_type')); $perpage = input::get('perpage', 10); $order = session::get('account.order', 'company_name.asc'); $order = explode('.', $order); $accounts = account::accounts($order, $account, $account_manager, $account_type)->paginate($perpage)->appends(array( 'account' => input::get('account'), 'account_manager' => input::get('account_manager'), 'account_type' => input::get('account_type'), 'perpage' => input::get('perpage') )); $accounts_trash = account::accountstrash($order)->get(); $message = session::get('message'); $default = ($perpage === null ? 10 : $perpage); $this->layout->content = view::make('admin.accounts.index', compact('accounts', 'accounts_trash', 'message', 'default')); }
and new getaccountbyname() method in controller used during ajax call. should go in model:
public function getaccountbyname() { $name = input::get('account'); return account::select(array('id', db::raw('company_name text')))->where('company_name', 'like', "%$name%")->get(); }
and 2 new methods in model retrieving accounts , accounts trash:
public function scopeaccounts($query, $order, $account, $account_manager, $account_type) { $query->leftjoin('users', 'users.id', '=', 'accounts.user_id') ->leftjoin('account_types', 'account_types.id', '=', 'accounts.account_type_id') ->orderby($order[0], $order[1]) ->select(array('accounts.*', db::raw('accounts.id aid'))); if (!empty($account[0])) { $query = $query->wherein('accounts.id', $account); } if (!empty($account_manager[0])) { $query = $query->wherein('users.id', $account_manager); } if (!empty($account_type[0])) { $query = $query->wherein('account_types.id', $account_type); } } public function scopeaccountstrash($query, $order) { $query->onlytrashed() ->leftjoin('users', 'users.id', '=', 'accounts.user_id') ->leftjoin('account_types', 'account_types.id', '=', 'accounts.account_type_id') ->orderby($order[0], $order[1]) ->select(array('accounts.*', db::raw('accounts.id aid'))); }
again, there's ton of things tidied here i'm closer faster , cleaner solution. doing has reduced loading times 12 seconds 234ms.
Comments
Post a Comment