Display large array in laravel blade in too much time - laravel

so i got imported my large excel file with huge informations with 1 sec and i did get all the information i need to display within 1 sec to , but i got a big probleme when displaying the array in the view coz i m testing if the cells are correct or not i m using 5 ifs and 3 foreach and it takes mote than 2 mins , i need help to display all the info in a short time this is my array , and thanks
and there is my code of the view which takes to much time to display infos
and thanks
//here we get our final result of true and false fields
$finale_array = [];
// here we get all our
$current_table2 = [];
$results_applied = [];
$current_result = [];
$columns = SCHEMA::getColumnListing('imports');
// here we get all our conditions
$conditions = DB::table('conditions')->select('number', 'field', 'value')->get();
// here we get all our data
$imports = DB::table('imports')->get();
$results = DB::table('results')->get();
$x = 0;
$default_value = 0;
foreach ($imports as $key => $imported) {
$res = get_object_vars($imported);
foreach ($conditions as $value) {
$array = get_object_vars($value);
$result = $this->test($columns, $array['field']); // the result of our test function
if ($result == "ok") {
if ($res[$array['field']] == $array['value']) {
foreach ($results as $value_result) {
$res_resultat = get_object_vars($value_result);
$test_field = $this->test($columns, $res_resultat['field']);
// testing if the condtion numder match with the result number
if (($res_resultat['condition_number'] == $array['number'])) {
if (($test_field == 'ok')) {
if ($res['id'] != $default_value) {
// here test if the difference between the id and the default value is different from the current id to insert
if (($res['id'] - $default_value) != $res['id']) {
array_push($current_table2, $results_applied);
array_push($finale_array, $current_table2);
$current_table2 = [];
$results_applied = [];
$default_value = $res['id'];
}
$current_table2 = [$res, $res['id']];
}
$current_result = [$res_resultat['field'], $res[$res_resultat['field']]];
if ($res_resultat['value'] == $res[$res_resultat['field']]) {
$current_result[2] = 'true';
$current_result[3] = $res_resultat['value'];
} else {
$current_result[2] = 'false';
$current_result[3] = $res_resultat['value'];
}
$current_result[4] = $array['number'];
array_push($results_applied, $current_result);
}
}
}
}
}
}
$default_value = $res['id'];
}
array_push($current_table2, $results_applied);
array_push($finale_array, $current_table2);
dd($finale_array);
return view('Appliedconditions', ['imports' => $finale_array, 'columns' => $columns]);
}

From what I can see, you are recovering all the data without paging it.
You must paginate the data using the paginate(#elements_per_page) directive in your query, where #elements_per_page is the number of elements you want to display per page.
For example:
$elements = Elements::select('*')->paginate(10);
and in your blade view you can retreive pagination links after the closing table tag in this way: {{ $elements->links() }}

Related

Elasticsearch slow extract data

I'm looking for the best method to export some datas from elasticsearch.
My index structure is very simple : ID + 4/5 simple fields
The ID is the ID of French companies,
The other fields are, for example, the activity code, the legal form code, employee size, zip code, ...
The total of records is about 30M.
The count is very fast, but if I want to extract only the IDs, it take a long time.
I need this list "online" for other treatments in a webpage.
How can I do this ?
My query for search is :
{"query":{"bool":{"must":[{"bool":{"should":[{"match":
{"etatadministratifunitelegale":"A"}}]}},{"bool":{"should":[{"match":
{"etatadministratifetablissement":"A"}}]}},{"bool":{"should":[{"match":
{"activiteprincipaleunitelegale":"68.20A"}}]}}], "must_not":[], "should":[] } } }
It's $params in my php code.
This query gives more than 650 000 IDs. To extract them :
$hosts = [
'http://xxxxxxxxx:9200'
];
$client = Elasticsearch\ClientBuilder::create()
->setHosts($hosts)
->build();
$xparams['index'] = 'vecteur';
$xparams['type'] = '_doc';
$xparams['stored_fields'] = [];
$xparams['scroll'] = '1s';
$xparams['size'] = '50000';
$xparams['body'] = $params;
$res = $client->search($xparams);
$scroll_id = $res['_scroll_id'];
$zzz = $res['hits']['hits'];
$xstr = "";
foreach ($zzz as $elt) {
$xstr .= $elt['_id']."\n";
}
fwrite($myfile, $xstr);
while (\true) {
$params2['scroll'] = '1s';
$params2['scroll_id'] = $scroll_id;
$response = $client->scroll($params2);
$zzz = $response['hits']['hits'];
$xstr = "";
foreach ($zzz as $elt) {
$xstr .= $elt['_id']."\n";
}
fwrite($myfile, $xstr);
if (count($response['hits']['hits']) > 0) {
$scroll_id = $response['_scroll_id'];
} else {
break;
}
}
fclose($myfile);
$myfile is a CSV file. Is it clear ?

Save multidimensional array using Laravel and ajax

I am building a pharmacy management system. I have a condition where I need to build a section
As shown in the picture, I need to store the data where the upper three should be same for all of the rows inputted below.
The form data is submitted as in the below picture.
But the data when looped and saved is not being saved as desired. Only the last row of the data is being inserted and I am also confused to store supplier, purchase date and note since these data should be repeated as many as the number of rows are added.
PurchaseController.php
public function storePurchase(PurchaseStoreRequest $request)
{
$purchase = new Purchase();
$count = count($request->name);
// return response()->json($request->all());
for ($i = 0; $i < $count; $i++) {
$purchase->supplier = $request->supplier;
$purchase->purchase_date = $request->purchase_date;
$purchase->description = $request->description;
$purchase->category = $request->category[$i];
$purchase->name = $request->name[$i];
$purchase->generic_name = $request->generic_name[$i];
$purchase->batch_number = $request->batch_number[$i];
$purchase->company = $request->company[$i];
$purchase->strength = $request->strength[$i];
$purchase->expiry_date = $request->expiry_date[$i];
$purchase->quantity = $request->quantity[$i];
$purchase->selling_price = $request->selling_price[$i];
$purchase->purchase_price = $request->purchase_price[$i];
$purchase->save();
}
return response()->json(['message' => 'Purchase Saved Successfully']);
}
Can someone help me to store these three fields in the database repeating the number of rows submitted ?
Currently only the last row is being inserted into the database.
This might be an another way to accomplish what you're looking for.
$sharedKeys = ['supplier', 'purchase_date', 'description'];
$sharedData = $request->only($sharedKeys);
$multiKeys = ['category', 'name', 'generic_name', 'batch_number', 'company', 'strength', 'expiry_date', 'quantity', 'selling_price', 'purchase_price'];
$multiData = $request->only($multiKeys);
for ($i = 0; $i < count($request->name); $i++) {
$individualData = array_combine($multiKeys, array_column($multiData, $i));
Purchase::create($sharedData + $individualData);
}
For every loop, you need to create a new instance, then It will create a new record on each loop :
public function storePurchase(PurchaseStoreRequest $request)
{
// $purchase = new Purchase();
$count = count($request->name);
// return response()->json($request->all());
for ($i = 0; $i < $count; $i++) {
$purchase = new Purchase(); // create new instance end with (); on each loop
$purchase->supplier = $request->supplier;
$purchase->purchase_date = $request->purchase_date;
$purchase->description = $request->description;
$purchase->category = $request->category[$i];
$purchase->name = $request->name[$i];
$purchase->generic_name = $request->generic_name[$i];
$purchase->batch_number = $request->batch_number[$i];
$purchase->company = $request->company[$i];
$purchase->strength = $request->strength[$i];
$purchase->expiry_date = $request->expiry_date[$i];
$purchase->quantity = $request->quantity[$i];
$purchase->selling_price = $request->selling_price[$i];
$purchase->purchase_price = $request->purchase_price[$i];
$purchase->save();
}
return response()->json(['message' => 'Purchase Saved Successfully']);
}

Why enter laravel loop data max 40 row?

I created a store function on my controller. At the time I will enter data with just one submit, only indexed up to 40 data will be entered. While those 40 and above do not enter the database. Why did this happen? What's the solution? Please i need help.
Sorry for my bad English.
Thanks
I am use Laravel 5.6
public function store(Request $request)
{
//
foreach($request->quantity as $quantity){
if($quantity == NULL){
}
if($quantity != NULL){
$data = new KomponenOlahan;
$data->quantity = $quantity;
$harga_satuan_mu = Input::get('harga_satuan_mu');
$total = $quantity*$harga_satuan_mu;
$data->kode_proyek = Input::get('kode_proyek');
$data->nama_proyek = Input::get('nama_proyek');
$data->kode_panel = Input::get('kode_panel');
$data->nama_panel = Input::get('nama_panel');
$data->schedule_kirim = Input::get('schedule_kirim');
$data->nama_sales = Input::get('nama_sales');
$data->ukuran_panel = Input::get('ukuran_panel');
$data->ref = Input::get('ref');
$data->type = Input::get('type');
$data->komponen_bantu = Input::get('komponen_bantu');
$data->nama_komponen = Input::get('nama_komponen');
$data->spek = Input::get('spek');
$data->satuan = Input::get('satuan');
$data->diskon = Input::get('diskon');
$data->harga = Input::get('harga');
$data->pole = Input::get('pole');
$data->ka = Input::get('ka');
$data->ampere = Input::get('ampere');
$data->quantity = $quantity;
$data->harga_satuan_mu = $harga_satuan_mu;
$data->harga_satuan_mb = Input::get('harga_satuan_mb');
$data->harga_satuan_lb_oh = Input::get('harga_satuan_lb_oh');
$data->id_estimasi = Input::get('id_estimasi');
$data->nama_estimasi = Input::get('nama_estimasi');
$data->total = $total;
$data->trigger_bom = Input::get('trigger_bom');
$data->save();
}
}
My View
<td><input type="text" name="quantity[]" class="form-control"></td>
<td><input type="text" name="kode_proyek" hidden value="{{$panel->kode_projek}}">
etc....
My View
my view
You can save it first to array then use bulk insert.
Inserting data 1 by 1 will slow your application.
$insertArray = array();
foreach($request->quantity as $quantity){
if($quantity == NULL){
$insertArray[] = array(
'quantity' => $quantity,
'harga_satuan_mu ' => Input::get('harga_satuan_mu'),
.... // add other fields
)
}
}
KomponenOlahan::insert($insertArray);

Laravel 5.3 database transaction issue?

I have used more time with Laravel 5.0 on database transaction but when I change to Laravel 5.3.* it not work as I want even I have disabled commit() method all data still continue insert into database.
if ($request->isMethod('post')) {
DB::beginTransaction();
$cats = new Cat();
$cats->parent_id = $this->request->input('category_id');
$cats->status = ($this->request->input('status')) ? $this->request->input('status') : 0;
if ($res['cat'] = $cats->save()) {
$catD = new CategoryDescriptions();
$catD->category_id = $cats->id;
$catD->language_id = 1;
$catD->name = $this->request->input('en_name');
if ($res['cat2'] = $catD->save()) {
$catD2 = new CategoryDescriptions();
$catD2->category_id = $cats->id;
$catD2->language_id = 2;
$catD2->name = $this->request->input('kh_name');
$res['all'] = $catD2->save();
}
}
if ($res) {
//DB::commit();
return $res;
}
return [false];
}
Check this line ($res['cat'] is a boolean):
if($res['cat'] = $cats->save()) {
And this ($res is an array. You compare an array as boolean!):
if($res){
The right condition should be:
$res = array(); // the first line of your method
// .... your http method check, start transaction, etc.
if (!in_array(false, $res, true)) { // check if array doesn't contain 'false values'
DB::commit();
}

CodeIgniter - unable to query database

I have written a function, and it's working except one part:
//Find current upload total value
$this->EE->db->select('upload_total');
$upload_total = $this->EE->db->get_where('exp_competition_purchase_upload_total', array('member_id' => $member_id));
return $upload_total->result();
// If upload total is more than 0
if($upload_total > 0) {
$new_total = $upload_total + $my_data['upload_total'];
$my_data['upload_total'] = $new_total;
}
Can anyone tell me if they can see any issue with this particular part of the code?
Here is the full function. It runs in an extension. The extension is working, it just appears to be the above code that isnt.
I am basically just trying to find the value in the db, then if the value is more than one, add the value to another.
function cartthrob_on_authorize()
{
foreach ($this->EE->cartthrob->cart->items() as $item) {
// Create array to store member id and purchased upload slots
$my_data = array(
'member_id' => $this->EE->session->userdata('member_id'),
'upload_total' => $item->item_options('fees'),
);
// Store member id in variable
$member_id = $my_data['member_id'];
// Query table to check if there is record with member id exists
$query = $this->EE->db->get_where('exp_competition_purchase_upload_total', array('member_id' => $member_id));
// If query returns more than 0 update record
if($query->num_rows() > 0) {
//Find current upload total value
$this->EE->db->select('upload_total');
$upload_total = $this->EE->db->get_where('exp_competition_purchase_upload_total', array('member_id' => $member_id));
// If upload total is more than 0
if($upload_total > 0) {
$new_total = $upload_total + $my_data['upload_total'];
$my_data['upload_total'] = $new_total;
}
return $upload_total->result();
$this->EE->db->where('member_id', $member_id);
$this->EE->db->update('exp_competition_purchase_upload_total', $my_data);
// If query returns 0 insert new record
} else {
$this->EE->db->insert('exp_competition_purchase_upload_total', $my_data);
}
}
}
//Find current upload total value
$this->EE->db->select('upload_total');
$upload_total = $this->EE->db->get_where('exp_competition_purchase_upload_total', array('member_id' => $member_id))->result_array();
$upload_total = $upload_total[0];
//If upload total is more than 0
$upload_total_col = $upload_total['upload_total']>0 ? $upload_total['upload_total']:0;
$my_data['upload_total'] += $upload_total_col;
From what I understand you, just want to increment upload_total.
Note: Don't use return $upload_total->result(); in between your function without putting a condition.
result of query is not an integer, it is an object
try like this;
//Find current upload total value
$this->EE->db->select('upload_total');
$upload_total = $this->EE->db->get_where('exp_competition_purchase_upload_total', array('member_id' => $member_id));
$result = $upload_total->result();
// If upload total is more than 0
if($upload_total->num_rows() > 0) {
$my_data['upload_total'] += $result->upload_total;
}
return $my_data;
btw, I'm not sure what you want to do, so i just corrected it!
here is the solution
$this->EE->db->select('upload_total');
$upload_total = $this->EE->db->get_where('exp_competition_purchase_upload_total', array('member_id' => $member_id));
// If upload total is more than 0
if($upload_total->num_rows() > 0) {
$result = $upload_total->row_array();
$new_total = $result['upload_total'] + $my_data['upload_total'];
$my_data['upload_total'] = $new_total;
}
return $my_data;
try that. it was becuase u was trying to use the DB returned object as an int.

Resources