0

Laravel 4 アプリの複数選択フィルター検索を開発する際に、ヘルプ/ガイダンスが必要です。

データベースに「アカウント」というテーブルがあります。このテーブルは、次の関係を介してデータベース内の他のテーブルにリンクされています。

'users' は属している (User モデルはアカウントに対して多数の関係を持っている) 'account_types' は属している (AccountType モデルはアカウントに対して 1 つの関係を持っている)

私の見解では、3 つの複数選択ボックス、会社名 (accounts テーブルの「company_name」フィールドから取得)、アカウント マネージャー (account_managers テーブルの「first_name」および「last_name」フィールドから取得)、およびアカウントの種類 (から取得) が必要です。 account_types テーブル、「タイプ」フィールド)。

ユーザーがこれらの複数選択ボックスから値を選択してフォームを送信すると、関連するテーブルを検索して結果を返す必要があります。非常に遅いため、これには結合を使用したくありません。特に、複数選択ボックスの値を戻す場合。

可能であれば、結果がすぐに返ってくるように Eloquent リレーションシップを使用したいと考えています。

これは結合とクエリ文字列で機能していますが、非常に遅く、最大で 10 ~ 15 秒です。

誰かがこれで私を助けてくれることを願っています。乾杯。

4

1 に答える 1

0

OK、すべての連絡先を一度にロードするだけでなく、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」が必要なため、これが大きな問題の 1 つだったと思います。

私のルートは単純でした:

Route::get('admin/get-contact', 'AdminContactsController@getContactByName');
于 2013-08-12T15:44:20.593 に答える