{"id":2860,"date":"2014-02-27T11:24:18","date_gmt":"2014-02-27T11:24:18","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/02\/27\/integrating-select2-with-laravel-4-app-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:45:33","modified_gmt":"2022-08-30T15:45:33","slug":"integrating-select2-with-laravel-4-app-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/02\/27\/integrating-select2-with-laravel-4-app-collection-of-common-programming-errors\/","title":{"rendered":"Integrating Select2 with Laravel 4 App-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;ve decided to implement select2 in my multiselect search filter and I could do with some help integrating it.<\/p>\n<p>I have the following method:<\/p>\n<pre><code>public function getContactByName($name)\n{\n    return Contact::select(array('id', DB::raw('concat(first_name,\" \",last_name) as name')))-&gt;where(DB::raw('concat(first_name,\" \",last_name)'), 'like', \"%$name%\")-&gt;get();\n}\n<\/code><\/pre>\n<p>This method returns any records where &#8216;first_name&#8217; and &#8216;last_name&#8217; are like the term entered in the URL, for example http:\/\/www.example.com\/admin\/get-contact-name\/{name}.<\/p>\n<p>The following route handles the request:<\/p>\n<pre><code>Route::get('admin\/get-contact-name\/{name}', 'AdminContactListController@getContactByName');\n<\/code><\/pre>\n<p>This all works great. I just need some help setting it up with select2 and AJAX. I&#8217;m not really sure what to put in the options for the JS.<\/p>\n<p>I have the following form field setup:<\/p>\n<pre><code>\n<\/code><\/pre>\n<p>Can anyone point me in the right direction. Cheers.<\/p>\n<p><strong>EDIT<\/strong>: I&#8217;m sort of playing about here and shooting in the dark, as I&#8217;m not sure what each of these elements mean, such as page etc but this is the code I have so far, suffice to say it&#8217;s not working:<\/p>\n<pre><code>var name = $('#contact_names_value').val();\n$('#contact_names_value').select2({\n    placeholder: 'Search contacts',\n    minimumInputLength: 3,\n    ajax: {\n        url: '\/admin\/get-contact-name' + name,\n        dataType: 'json',\n        data: function (term, page) {\n            return {\n                q: term,\n                page_limit: 10\n            };\n        },\n        results: function (data, page) {\n            return {results: data};\n        }\n    },\n});\n<\/code><\/pre>\n<p><strong>EDIT<\/strong>: OK guys, I think I&#8217;m much closer, and I think I&#8217;m getting results via select2 now but I&#8217;m getting a JavaScript error. Here&#8217;s what I have so far.<\/p>\n<p>Method:<\/p>\n<pre><code>public function getContactByName()\n{\n    $name = Input::get('name');\n    return Contact::select(array('id', DB::raw('concat(first_name,\" \",last_name) as name')))-&gt;where(DB::raw('concat(first_name,\" \",last_name)'), 'like', \"%$name%\")-&gt;get();\n}\n<\/code><\/pre>\n<p>Route:<\/p>\n<pre><code>Route::get('admin\/get-contact', 'AdminContactListController@getContactByName');\n<\/code><\/pre>\n<p>Select2 JavaScript:<\/p>\n<pre><code>$('#contact_names_value').select2({\n    name: $('.select2-input').val(),\n    placeholder: 'Search contacts',\n    minimumInputLength: 3,\n    ajax: {\n        url: '\/admin\/get-contact' + name,\n        dataType: 'json',\n        data: function (term) {\n            return {\n                name: term\n            };\n        },\n        results: function (data) {\n            return {results: data};\n        },\n        dropdownCssClass: 'bigdrop'\n    },\n});\n<\/code><\/pre>\n<p>Select2 JS Error:<\/p>\n<pre><code>Uncaught TypeError: Cannot call method 'toUpperCase' of undefined plugins.js:1549\nC plugins.js:1549\na.fn.select2.defaults.formatResult plugins.js:1550\ng plugins.js:1549\na.extend.populateResults plugins.js:1549\nf.query.callback plugins.js:1549\n(anonymous function) plugins.js:1549\na.extend.success plugins.js:1549\nc jquery-1.9.1.min.js:3\np.fireWith jquery-1.9.1.min.js:3\nk jquery-1.9.1.min.js:5\nr\n<\/code><\/pre>\n<p>Any advice guys?<\/p>\n<ol>\n<li>\n<p>OK, I have this working like a charm now.<\/p>\n<p>Here&#8217;s my JS code:<\/p>\n<pre><code>$('#contact_names_value').select2({\n    placeholder: 'Search contacts',\n    minimumInputLength: 3,\n    ajax: {\n        url: '\/admin\/get-contact',\n        dataType: 'json',\n        data: function (term, page) {\n            return {\n                contact_names_value: term\n            };\n        },\n        results: function (data, page) {\n            return {results: data};\n        }\n    },\n    tags: true\n});\n<\/code><\/pre>\n<p>I did not need the &#8216;name&#8217; variable reference, as the plugin handles this and appends the value of the select\/input to the query string.<\/p>\n<p>Secondly, in my form, I needed to set the type to &#8216;hidden&#8217;, as previously it was text.<\/p>\n<p>You&#8217;ll also notice above that I added &#8216;page&#8217; to both the results and data functions. Not sure if this made a difference.<\/p>\n<p>I also added &#8216;tags: true&#8217; to make it work like a multiselect.<\/p>\n<p>Here&#8217;s my method code:<\/p>\n<pre><code>public function getContactByName()\n{\n    $name = Input::get('contact_names_value');\n    return Contact::select(array('id', DB::raw('concat(first_name,\" \",last_name) as text')))-&gt;where(DB::raw('concat(first_name,\" \",last_name)'), 'like', \"%$name%\")-&gt;get();\n}\n<\/code><\/pre>\n<p>Notice during my select statement, I do a DB::raw and set the &#8216;first_name&#8217; and &#8216;last_name&#8217; fields to be selected as &#8216;text&#8217;. I think this was one of the major issues, as the plugin requires &#8216;id&#8217; and &#8216;text&#8217; to function.<\/p>\n<p>My route was simply:<\/p>\n<pre><code>Route::get('admin\/get-contact', 'AdminContactsController@getContactByName');\n<\/code><\/pre>\n<\/li>\n<li>\n<p>You simply grab the input box (e.g. using jQuery you can do $(&#8216;#contact_names_value&#8217;) or javascript is document.getElementById(&#8216;contact_names_value&#8217;).)<\/p>\n<p>An example that should work:<\/p>\n<pre><code>$('#contact_names_value').select2(your_value_from_getContactByName_method_here);\n<\/code><\/pre>\n<p>Hope that helps.<\/p>\n<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve decided to implement select2 in my multiselect search filter and I could do with some help integrating it. I have the following method: public function getContactByName($name) { return Contact::select(array(&#8216;id&#8217;, DB::raw(&#8216;concat(first_name,&#8221; &#8220;,last_name) as name&#8217;)))-&gt;where(DB::raw(&#8216;concat(first_name,&#8221; &#8220;,last_name)&#8217;), &#8216;like&#8217;, &#8220;%$name%&#8221;)-&gt;get(); } This method returns any records where &#8216;first_name&#8217; and &#8216;last_name&#8217; are like the term entered in the URL, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,1],"tags":[],"class_list":["post-2860","post","type-post","status-publish","format-standard","hentry","category-laravel","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2860","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=2860"}],"version-history":[{"count":1,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2860\/revisions"}],"predecessor-version":[{"id":8845,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2860\/revisions\/8845"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=2860"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=2860"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=2860"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}