How to optimize the load of many data options into a select box from db passed via a variable in Laravel 5.7?










0















I'm populating a select box with data from a database. The thing is that there are 139659 elements inside it (just getting from a single column).



I tried it once and I guess all the data got loaded, and of course my application got slow loading all that stuff.



So, how could I optimize the data fetch from it? I will use select2 to get more functionalities, though I think I first need to address how to load the data.



My code:



<div id="inputs-postal">
<div class="form-group">
<select name="codigoPostal" id="select-cp">
@foreach ($cpostales as $cp)
<option value=" $cp->postal_code "> $cp->postal_code </option>
@endforeach
</select>
</div>
</div>


Controller:



public function create()


// Toma los estados de la base de datos.

$estados = DB::connection('db_postalcodes')
->table('state')
->groupBy('state')
->get();
$cpostales = DB::connection('db_postalcodes')
->table('postal_code')
->get();

// El with hace que se adjunten variables al view.
return view('admin.posts.create')->with('estados', $estados)->with('cpostales', $cpostales);










share|improve this question

















  • 1





    I would completely rethink what it is you are trying to accomplish. There is never a reason to load that many items into a select box. I would use some ajax and have them select through categories that narrows the results into smaller chunks of data.

    – Joseph_J
    Nov 16 '18 at 1:58












  • Yeah, I know, though, could you guide me on that? Novice here. Any tutorial or something would be appreciated, involving Laravel of course.

    – dawn
    Nov 16 '18 at 1:59






  • 1





    Google these keywords: dynamic populating javascript ajax jquery.

    – Joseph_J
    Nov 16 '18 at 2:04















0















I'm populating a select box with data from a database. The thing is that there are 139659 elements inside it (just getting from a single column).



I tried it once and I guess all the data got loaded, and of course my application got slow loading all that stuff.



So, how could I optimize the data fetch from it? I will use select2 to get more functionalities, though I think I first need to address how to load the data.



My code:



<div id="inputs-postal">
<div class="form-group">
<select name="codigoPostal" id="select-cp">
@foreach ($cpostales as $cp)
<option value=" $cp->postal_code "> $cp->postal_code </option>
@endforeach
</select>
</div>
</div>


Controller:



public function create()


// Toma los estados de la base de datos.

$estados = DB::connection('db_postalcodes')
->table('state')
->groupBy('state')
->get();
$cpostales = DB::connection('db_postalcodes')
->table('postal_code')
->get();

// El with hace que se adjunten variables al view.
return view('admin.posts.create')->with('estados', $estados)->with('cpostales', $cpostales);










share|improve this question

















  • 1





    I would completely rethink what it is you are trying to accomplish. There is never a reason to load that many items into a select box. I would use some ajax and have them select through categories that narrows the results into smaller chunks of data.

    – Joseph_J
    Nov 16 '18 at 1:58












  • Yeah, I know, though, could you guide me on that? Novice here. Any tutorial or something would be appreciated, involving Laravel of course.

    – dawn
    Nov 16 '18 at 1:59






  • 1





    Google these keywords: dynamic populating javascript ajax jquery.

    – Joseph_J
    Nov 16 '18 at 2:04













0












0








0








I'm populating a select box with data from a database. The thing is that there are 139659 elements inside it (just getting from a single column).



I tried it once and I guess all the data got loaded, and of course my application got slow loading all that stuff.



So, how could I optimize the data fetch from it? I will use select2 to get more functionalities, though I think I first need to address how to load the data.



My code:



<div id="inputs-postal">
<div class="form-group">
<select name="codigoPostal" id="select-cp">
@foreach ($cpostales as $cp)
<option value=" $cp->postal_code "> $cp->postal_code </option>
@endforeach
</select>
</div>
</div>


Controller:



public function create()


// Toma los estados de la base de datos.

$estados = DB::connection('db_postalcodes')
->table('state')
->groupBy('state')
->get();
$cpostales = DB::connection('db_postalcodes')
->table('postal_code')
->get();

// El with hace que se adjunten variables al view.
return view('admin.posts.create')->with('estados', $estados)->with('cpostales', $cpostales);










share|improve this question














I'm populating a select box with data from a database. The thing is that there are 139659 elements inside it (just getting from a single column).



I tried it once and I guess all the data got loaded, and of course my application got slow loading all that stuff.



So, how could I optimize the data fetch from it? I will use select2 to get more functionalities, though I think I first need to address how to load the data.



My code:



<div id="inputs-postal">
<div class="form-group">
<select name="codigoPostal" id="select-cp">
@foreach ($cpostales as $cp)
<option value=" $cp->postal_code "> $cp->postal_code </option>
@endforeach
</select>
</div>
</div>


Controller:



public function create()


// Toma los estados de la base de datos.

$estados = DB::connection('db_postalcodes')
->table('state')
->groupBy('state')
->get();
$cpostales = DB::connection('db_postalcodes')
->table('postal_code')
->get();

// El with hace que se adjunten variables al view.
return view('admin.posts.create')->with('estados', $estados)->with('cpostales', $cpostales);







php laravel






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 16 '18 at 1:07









dawndawn

3371518




3371518







  • 1





    I would completely rethink what it is you are trying to accomplish. There is never a reason to load that many items into a select box. I would use some ajax and have them select through categories that narrows the results into smaller chunks of data.

    – Joseph_J
    Nov 16 '18 at 1:58












  • Yeah, I know, though, could you guide me on that? Novice here. Any tutorial or something would be appreciated, involving Laravel of course.

    – dawn
    Nov 16 '18 at 1:59






  • 1





    Google these keywords: dynamic populating javascript ajax jquery.

    – Joseph_J
    Nov 16 '18 at 2:04












  • 1





    I would completely rethink what it is you are trying to accomplish. There is never a reason to load that many items into a select box. I would use some ajax and have them select through categories that narrows the results into smaller chunks of data.

    – Joseph_J
    Nov 16 '18 at 1:58












  • Yeah, I know, though, could you guide me on that? Novice here. Any tutorial or something would be appreciated, involving Laravel of course.

    – dawn
    Nov 16 '18 at 1:59






  • 1





    Google these keywords: dynamic populating javascript ajax jquery.

    – Joseph_J
    Nov 16 '18 at 2:04







1




1





I would completely rethink what it is you are trying to accomplish. There is never a reason to load that many items into a select box. I would use some ajax and have them select through categories that narrows the results into smaller chunks of data.

– Joseph_J
Nov 16 '18 at 1:58






I would completely rethink what it is you are trying to accomplish. There is never a reason to load that many items into a select box. I would use some ajax and have them select through categories that narrows the results into smaller chunks of data.

– Joseph_J
Nov 16 '18 at 1:58














Yeah, I know, though, could you guide me on that? Novice here. Any tutorial or something would be appreciated, involving Laravel of course.

– dawn
Nov 16 '18 at 1:59





Yeah, I know, though, could you guide me on that? Novice here. Any tutorial or something would be appreciated, involving Laravel of course.

– dawn
Nov 16 '18 at 1:59




1




1





Google these keywords: dynamic populating javascript ajax jquery.

– Joseph_J
Nov 16 '18 at 2:04





Google these keywords: dynamic populating javascript ajax jquery.

– Joseph_J
Nov 16 '18 at 2:04












1 Answer
1






active

oldest

votes


















1














One option if you don't mind using jQuery UI is to use jQuery autocomplete with a text field, instead of a drop-down. Here's some working code I used.



In your controller, create a function that returns a small number of postal codes:



<?php

use Response;

...


public function searchPostalCode(Request $request)

$term = $request->input('term');

$results = ;

$queries = DB::table('db_postalcodes')
->where('postal_code', 'LIKE', '%'.$term.'%') //search 'postal_code' column
->take(5)->get(); // get 5 results

foreach ($queries as $query)
$results = ['id' => $query->id, 'value' => $query->name];


return Response::json($results);



Define a route for it, in Laravel's web routes



Route::get('/searchPostalCode', 'PostalCodeController@searchPostalCode');


Then, in your blade template, add this Snippet





<input id="postal-code" type="text" name="postal-code">

<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

<script type="text/javascript">

<!--
$( "#postal-code" ).autocomplete(
source: "URL('/searchPostalCode')",
minLength: 3,
select: function(event, ui)
$('#postal-code').val(ui.item.value);

);
//-->
</script>





share|improve this answer

























  • thanks! what does the '%' mean?

    – dawn
    Nov 16 '18 at 4:35







  • 1





    It's explained here: w3schools.com/sql/sql_like.asp @dawn

    – hktang
    Nov 16 '18 at 4:45












  • I'm implementing your code now, but I'm getting a GET http://apr2.test/buscarCodigoPostal?term=200 404 (Not Found) error. I'm looking at it, but maybe you know already what's happening ?¿

    – dawn
    Nov 16 '18 at 5:01











  • I wrote it outside of a ::group I had, but now I'm getting a GET http://apr2.test/searchPostalCode?term=200 500 (Internal Server Error). Any idea what could it be?

    – dawn
    Nov 16 '18 at 5:10







  • 1





    @dawn hmm. not sure. Also make sure you include "Response" at the top of the controller by adding use Response; after use DB; etc. Will update my answer.

    – hktang
    Nov 16 '18 at 5:43











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53330040%2fhow-to-optimize-the-load-of-many-data-options-into-a-select-box-from-db-passed-v%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














One option if you don't mind using jQuery UI is to use jQuery autocomplete with a text field, instead of a drop-down. Here's some working code I used.



In your controller, create a function that returns a small number of postal codes:



<?php

use Response;

...


public function searchPostalCode(Request $request)

$term = $request->input('term');

$results = ;

$queries = DB::table('db_postalcodes')
->where('postal_code', 'LIKE', '%'.$term.'%') //search 'postal_code' column
->take(5)->get(); // get 5 results

foreach ($queries as $query)
$results = ['id' => $query->id, 'value' => $query->name];


return Response::json($results);



Define a route for it, in Laravel's web routes



Route::get('/searchPostalCode', 'PostalCodeController@searchPostalCode');


Then, in your blade template, add this Snippet





<input id="postal-code" type="text" name="postal-code">

<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

<script type="text/javascript">

<!--
$( "#postal-code" ).autocomplete(
source: "URL('/searchPostalCode')",
minLength: 3,
select: function(event, ui)
$('#postal-code').val(ui.item.value);

);
//-->
</script>





share|improve this answer

























  • thanks! what does the '%' mean?

    – dawn
    Nov 16 '18 at 4:35







  • 1





    It's explained here: w3schools.com/sql/sql_like.asp @dawn

    – hktang
    Nov 16 '18 at 4:45












  • I'm implementing your code now, but I'm getting a GET http://apr2.test/buscarCodigoPostal?term=200 404 (Not Found) error. I'm looking at it, but maybe you know already what's happening ?¿

    – dawn
    Nov 16 '18 at 5:01











  • I wrote it outside of a ::group I had, but now I'm getting a GET http://apr2.test/searchPostalCode?term=200 500 (Internal Server Error). Any idea what could it be?

    – dawn
    Nov 16 '18 at 5:10







  • 1





    @dawn hmm. not sure. Also make sure you include "Response" at the top of the controller by adding use Response; after use DB; etc. Will update my answer.

    – hktang
    Nov 16 '18 at 5:43
















1














One option if you don't mind using jQuery UI is to use jQuery autocomplete with a text field, instead of a drop-down. Here's some working code I used.



In your controller, create a function that returns a small number of postal codes:



<?php

use Response;

...


public function searchPostalCode(Request $request)

$term = $request->input('term');

$results = ;

$queries = DB::table('db_postalcodes')
->where('postal_code', 'LIKE', '%'.$term.'%') //search 'postal_code' column
->take(5)->get(); // get 5 results

foreach ($queries as $query)
$results = ['id' => $query->id, 'value' => $query->name];


return Response::json($results);



Define a route for it, in Laravel's web routes



Route::get('/searchPostalCode', 'PostalCodeController@searchPostalCode');


Then, in your blade template, add this Snippet





<input id="postal-code" type="text" name="postal-code">

<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

<script type="text/javascript">

<!--
$( "#postal-code" ).autocomplete(
source: "URL('/searchPostalCode')",
minLength: 3,
select: function(event, ui)
$('#postal-code').val(ui.item.value);

);
//-->
</script>





share|improve this answer

























  • thanks! what does the '%' mean?

    – dawn
    Nov 16 '18 at 4:35







  • 1





    It's explained here: w3schools.com/sql/sql_like.asp @dawn

    – hktang
    Nov 16 '18 at 4:45












  • I'm implementing your code now, but I'm getting a GET http://apr2.test/buscarCodigoPostal?term=200 404 (Not Found) error. I'm looking at it, but maybe you know already what's happening ?¿

    – dawn
    Nov 16 '18 at 5:01











  • I wrote it outside of a ::group I had, but now I'm getting a GET http://apr2.test/searchPostalCode?term=200 500 (Internal Server Error). Any idea what could it be?

    – dawn
    Nov 16 '18 at 5:10







  • 1





    @dawn hmm. not sure. Also make sure you include "Response" at the top of the controller by adding use Response; after use DB; etc. Will update my answer.

    – hktang
    Nov 16 '18 at 5:43














1












1








1







One option if you don't mind using jQuery UI is to use jQuery autocomplete with a text field, instead of a drop-down. Here's some working code I used.



In your controller, create a function that returns a small number of postal codes:



<?php

use Response;

...


public function searchPostalCode(Request $request)

$term = $request->input('term');

$results = ;

$queries = DB::table('db_postalcodes')
->where('postal_code', 'LIKE', '%'.$term.'%') //search 'postal_code' column
->take(5)->get(); // get 5 results

foreach ($queries as $query)
$results = ['id' => $query->id, 'value' => $query->name];


return Response::json($results);



Define a route for it, in Laravel's web routes



Route::get('/searchPostalCode', 'PostalCodeController@searchPostalCode');


Then, in your blade template, add this Snippet





<input id="postal-code" type="text" name="postal-code">

<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

<script type="text/javascript">

<!--
$( "#postal-code" ).autocomplete(
source: "URL('/searchPostalCode')",
minLength: 3,
select: function(event, ui)
$('#postal-code').val(ui.item.value);

);
//-->
</script>





share|improve this answer















One option if you don't mind using jQuery UI is to use jQuery autocomplete with a text field, instead of a drop-down. Here's some working code I used.



In your controller, create a function that returns a small number of postal codes:



<?php

use Response;

...


public function searchPostalCode(Request $request)

$term = $request->input('term');

$results = ;

$queries = DB::table('db_postalcodes')
->where('postal_code', 'LIKE', '%'.$term.'%') //search 'postal_code' column
->take(5)->get(); // get 5 results

foreach ($queries as $query)
$results = ['id' => $query->id, 'value' => $query->name];


return Response::json($results);



Define a route for it, in Laravel's web routes



Route::get('/searchPostalCode', 'PostalCodeController@searchPostalCode');


Then, in your blade template, add this Snippet





<input id="postal-code" type="text" name="postal-code">

<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

<script type="text/javascript">

<!--
$( "#postal-code" ).autocomplete(
source: "URL('/searchPostalCode')",
minLength: 3,
select: function(event, ui)
$('#postal-code').val(ui.item.value);

);
//-->
</script>






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 16 '18 at 6:25

























answered Nov 16 '18 at 3:16









hktanghktang

9941530




9941530












  • thanks! what does the '%' mean?

    – dawn
    Nov 16 '18 at 4:35







  • 1





    It's explained here: w3schools.com/sql/sql_like.asp @dawn

    – hktang
    Nov 16 '18 at 4:45












  • I'm implementing your code now, but I'm getting a GET http://apr2.test/buscarCodigoPostal?term=200 404 (Not Found) error. I'm looking at it, but maybe you know already what's happening ?¿

    – dawn
    Nov 16 '18 at 5:01











  • I wrote it outside of a ::group I had, but now I'm getting a GET http://apr2.test/searchPostalCode?term=200 500 (Internal Server Error). Any idea what could it be?

    – dawn
    Nov 16 '18 at 5:10







  • 1





    @dawn hmm. not sure. Also make sure you include "Response" at the top of the controller by adding use Response; after use DB; etc. Will update my answer.

    – hktang
    Nov 16 '18 at 5:43


















  • thanks! what does the '%' mean?

    – dawn
    Nov 16 '18 at 4:35







  • 1





    It's explained here: w3schools.com/sql/sql_like.asp @dawn

    – hktang
    Nov 16 '18 at 4:45












  • I'm implementing your code now, but I'm getting a GET http://apr2.test/buscarCodigoPostal?term=200 404 (Not Found) error. I'm looking at it, but maybe you know already what's happening ?¿

    – dawn
    Nov 16 '18 at 5:01











  • I wrote it outside of a ::group I had, but now I'm getting a GET http://apr2.test/searchPostalCode?term=200 500 (Internal Server Error). Any idea what could it be?

    – dawn
    Nov 16 '18 at 5:10







  • 1





    @dawn hmm. not sure. Also make sure you include "Response" at the top of the controller by adding use Response; after use DB; etc. Will update my answer.

    – hktang
    Nov 16 '18 at 5:43

















thanks! what does the '%' mean?

– dawn
Nov 16 '18 at 4:35






thanks! what does the '%' mean?

– dawn
Nov 16 '18 at 4:35





1




1





It's explained here: w3schools.com/sql/sql_like.asp @dawn

– hktang
Nov 16 '18 at 4:45






It's explained here: w3schools.com/sql/sql_like.asp @dawn

– hktang
Nov 16 '18 at 4:45














I'm implementing your code now, but I'm getting a GET http://apr2.test/buscarCodigoPostal?term=200 404 (Not Found) error. I'm looking at it, but maybe you know already what's happening ?¿

– dawn
Nov 16 '18 at 5:01





I'm implementing your code now, but I'm getting a GET http://apr2.test/buscarCodigoPostal?term=200 404 (Not Found) error. I'm looking at it, but maybe you know already what's happening ?¿

– dawn
Nov 16 '18 at 5:01













I wrote it outside of a ::group I had, but now I'm getting a GET http://apr2.test/searchPostalCode?term=200 500 (Internal Server Error). Any idea what could it be?

– dawn
Nov 16 '18 at 5:10






I wrote it outside of a ::group I had, but now I'm getting a GET http://apr2.test/searchPostalCode?term=200 500 (Internal Server Error). Any idea what could it be?

– dawn
Nov 16 '18 at 5:10





1




1





@dawn hmm. not sure. Also make sure you include "Response" at the top of the controller by adding use Response; after use DB; etc. Will update my answer.

– hktang
Nov 16 '18 at 5:43






@dawn hmm. not sure. Also make sure you include "Response" at the top of the controller by adding use Response; after use DB; etc. Will update my answer.

– hktang
Nov 16 '18 at 5:43




















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53330040%2fhow-to-optimize-the-load-of-many-data-options-into-a-select-box-from-db-passed-v%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

ReactJS Fetched API data displays live - need Data displayed static

Evgeni Malkin