【问题标题】:Multi-select Filter Search in Laravel 4Laravel 4 中的多选过滤器搜索
【发布时间】:2013-08-10 05:25:12
【问题描述】:

在为我的 Laravel 4 应用程序开发多选过滤器搜索时需要帮助/指导。

我的数据库中有一个名为“帐户”的表。该表通过以下关系链接到数据库中的其他表:

'users' via 属于(用户模型与帐户有很多关系) 'account_types' via 属于(AccountType 模型与帐户有一个关系)

在我看来,我想要 3 个多选框、公司名称(取自账户表的“公司名称”字段)、客户经理(取自账户经理表、“名字”和“姓氏”字段)和账户类型(取自 account_types 表的 'type' 字段)。

当用户从这些多选框中选择值并提交表单时,我需要搜索相关表格并返回结果。我不想为此使用连接,因为它非常慢。尤其是在为多选框取回值时。

如果可能的话,我想以一种能够快速恢复结果的方式使用 Eloquent 关系。

我可以使用连接和查询字符串,但它非常慢,最多 10 到 15 秒。

我希望有人可以帮助我解决这个问题。干杯。

【问题讨论】:

  • 你能发布你当前的代码来进行需要 10 到 15 秒的搜索吗?另一方面,您是否对结果进行了分页?
  • 您是否尝试过急切加载您的关系?

标签: mysql laravel laravel-4 eloquent query-builder


【解决方案1】:

好的,我现在通过实现 select2 让这个工作像魅力一样,而不是简单地一次性加载所有联系人。效果也更好。

这是我在 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 the view, there is a dropdown box, that allows the user to select the amount of records to show per page. Retrive that value or set a default.
    $perPage = Input::get('perPage', 10);

    // This code retrieves the order from  that has been selected by the user by clicking on table ciolumn titles. The value is placed in the session and is used later in the Eloquent query and 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
    ));
}

我的 Contact.php 模型中的 scopeContacts 方法:

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 as cid'), DB::raw('CONCAT(contacts.first_name," ",contacts.last_name) as cname'), DB::raw('CONCAT(users.first_name," ",users.last_name) as 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);
    }
}

这是我的 JS 代码:

$('#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
});

这是我在 AdminContactsController.php 中实现的方法 getContactByName(为用户和帐户实现的类似方法)代码:

public function getContactByName()
{
    $name = Input::get('contact_names_value');
    return Contact::select(array('id', DB::raw('concat(first_name," ",last_name) as text')))->where(DB::raw('concat(first_name," ",last_name)'), 'like', "%$name%")->get();
}

请注意,在我的 select 语句中,我执行 DB::raw 并将“first_name”和“last_name”字段设置为“text”。我认为这是主要问题之一,因为插件需要“id”和“text”才能运行。

我的路线很简单:

Route::get('admin/get-contact', 'AdminContactsController@getContactByName');

【讨论】:

    猜你喜欢
    • 2020-10-17
    • 1970-01-01
    • 1970-01-01
    • 2017-02-14
    • 1970-01-01
    • 1970-01-01
    • 2018-04-08
    • 1970-01-01
    相关资源
    最近更新 更多