mysql - Multi-select Filter Search in Laravel 4 -
i need help/guidance in developing multi-select filter search laravel 4 app.
i have table in database called 'accounts'. table linked other tables in database via following relationships:
'users' via belongs (user model has has many relationship accounts) 'account_types' via belongs (accounttype model has has 1 relationship accounts)
in view 3 multi-select boxes, company names (taken accounts table 'company_name' field), account managers (taken account_managers table, 'first_name' , 'last_name' fields) , account type (taken account_types table, 'type' field).
when user selects values these multi-select boxes , submits form, need search relevant tables , bring results. don't want use joins this, slow. especially, when bringing values multi-select boxes.
if possible use eloquent relationships in way brings results quickly.
i have working joins , query strings slow, 10 15 seconds.
i hope can me out this. cheers.
ok, have working charm implementing select2, rather loading contacts in 1 go. works nicer too.
here's index method in admincontactscontroller.php:
public function index() { $contact_names_value = explode(',', input::get('contact_names_value')); $accounts_value = explode(',', input::get('accounts_value')); $account_managers_value = explode(',', input::get('account_managers_value')); // in view, there dropdown box, allows user select amount of records show per page. retrive value or set default. $perpage = input::get('perpage', 10); // code retrieves order has been selected user clicking on table ciolumn titles. value placed in session , used later in eloquent query , joins. $order = session::get('contact.order', 'cname.asc'); $order = explode('.', $order); $message = session::get('message'); $default = ($perpage === null ? 10 : $perpage); $contacts_trash = contact::contactstrash($order)->get(); $this->layout->content = view::make('admin.contacts.index', array( 'contacts' => contact::contacts($order, $contact_names_value, $accounts_value, $account_managers_value, $perpage)->paginate($perpage)->appends(array('accounts_value' => input::get('accounts_value'), 'account_managers_value' => input::get('account_managers_value'))), 'contacts_trash' => $contacts_trash, 'perpage' => $perpage, 'message' => $message, 'default' => $default )); } my scopecontacts method in contact.php model:
public function scopecontacts($query, $order, $contact_names_value, $accounts_value, $account_managers_value, $perpage) { $query->leftjoin('accounts', 'accounts.id', '=', 'contacts.account_id') ->leftjoin('users', 'users.id', '=', 'accounts.user_id') ->orderby($order[0], $order[1]) ->select(array('contacts.*', db::raw('contacts.id cid'), db::raw('concat(contacts.first_name," ",contacts.last_name) cname'), db::raw('concat(users.first_name," ",users.last_name) amname'))); if (empty($contact_names_value[0])) { // } else { $query = $query->wherein('contacts.id', $contact_names_value); } if (empty($accounts_value[0])) { // } else { $query = $query->wherein('accounts.id', $accounts_value); } if (empty($account_managers_value[0])) { // } else { $query->wherein('users.id', $account_managers_value); } } here's js code:
$('#contact_names_value').select2({ placeholder: 'search contacts', minimuminputlength: 3, ajax: { url: '/admin/get-contact', datatype: 'json', data: function (term, page) { return { contact_names_value: term }; }, results: function (data, page) { return {results: data}; } }, tags: true }); here's method getcontactbyname implemented in admincontactscontroller.php (similar methods implemented users , accounts) code:
public function getcontactbyname() { $name = input::get('contact_names_value'); return contact::select(array('id', db::raw('concat(first_name," ",last_name) text')))->where(db::raw('concat(first_name," ",last_name)'), 'like', "%$name%")->get(); } notice during select statement, db::raw , set 'first_name' , 'last_name' fields selected 'text'. think 1 of major issues, plugin requires 'id' , 'text' function.
my route simply:
route::get('admin/get-contact', 'admincontactscontroller@getcontactbyname');
Comments
Post a Comment