gpt4 book ai didi

mysql - Laravel 4 中的多选过滤器搜索

转载 作者:行者123 更新时间:2023-11-30 01:24:49 24 4
gpt4 key购买 nike

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

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

“users”via 属于(用户模型与帐户有很多关系)'account_types' via 属于(AccountType 模型与帐户具有一对一的关系)

在我看来,我想要 3 个多选框、公司名称(取自帐户表“company_name”字段)、帐户经理(取自 account_managers 表“first_name”和“last_name”字段)和帐户类型(取自 account_types 表的“类型”字段)。

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

如果可能的话,我希望使用 Eloquent 关系来快速返回结果。

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

我希望有人能帮我解决这个问题。干杯。

最佳答案

好的,我现在通过实现 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');

关于mysql - Laravel 4 中的多选过滤器搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18127040/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com