I have this variable called $projectFieldOptions and it's output is like this:
https://prnt.sc/7HtxrfTy9HiI.
Now, In the Controller I need to update this. What I am doing this, first delete all the existing rows based on id_feed and id_project and then loop through this variable $projectFieldOptions and insert it. Like this:
if( $request->feed_type !== 'scrape' ) {
$delete_mapping = DB::connection($db_name)->table($db_name . '.feed_mappings')
->where('id_feed', '=', $id_feed)
->where('id_project', '=', $token)
->delete();
}
// now insert
$field_mapping = true;
if( $request->feed_type !== 'scrape' ) {
if( count($projectFieldOptions) ) {
foreach ($projectFieldOptions as $mapping) {
$data[] = [
'id_feed' => $id_feed,
'id_project' => $token,
'import_field_slug' => $mapping['value'],
'internal_field_slug' => $mapping['custom'] ? $mapping['custom_field'] : $mapping['text'],
'custom_field' => $mapping['custom'],
'updates' => $mapping['updates'],
'removes' => $mapping['removes'],
'import' => 1,
'date_add' => now(),
'date_upd' => now()
];
}
} else {
$data = [];
}
$field_mapping = DB::connection($db_name)->table($db_name . ".feed_mappings")->insert($data);
}
Now, I don't want to delete existing rows instead I want to update those rows based on the id_feed_mappings. Can you tell how can I do this?
Check if this would work, to update based on id_feed_mappings value, you can use the ->where('id_feed_mappings', '=' ,'a value or variable') before ->update($data)
if( $request->feed_type !== 'scrape' ) {
// try using update instead of insert
$field_mapping = true;
if( $request->feed_type !== 'scrape' ) {
if( count($projectFieldOptions) ) {
foreach ($projectFieldOptions as $mapping) {
$data[] = [
'id_feed' => $id_feed,
'id_project' => $token,
'import_field_slug' => $mapping['value'],
'internal_field_slug' => $mapping['custom'] ? $mapping['custom_field'] : $mapping['text'],
'custom_field' => $mapping['custom'],
'updates' => $mapping['updates'],
'removes' => $mapping['removes'],
'import' => 1,
'date_add' => now(),
'date_upd' => now()
];
}
} else {
$data = [];
}
$field_mapping = DB::connection($db_name)->table($db_name . ".feed_mappings")->update($data);
}
Related
I have a pivot table with extra fields ('tugas_1'-'tugas_10') with enum type, the point of this update is to modify those fields.
the problem is if the user open the modal form and then submit it with no changes, the pages goes blank but if I change one or more of the fields, it works. this happens if I only use the updateExistingPivot method so I solve this by checking first if the old value is the same as the request form value, then it will run the sync method. but is there any other way to solve this? and my code looks like a mess, I appreciate it if you know how to make it simple
this is my edit method on the controller.
public function edit(Request $request, $id_angkatan, $id_semester, $id) {
$peserta = Peserta::where('id', $id)->first();
$tugas = $peserta->semestertugas()->where('semester_id', $id_semester)->first();
if($tugas->pivot->tugas_1 == $request->tugas_1 &&
$tugas->pivot->tugas_2 == $request->tugas_2 &&
$tugas->pivot->tugas_3 == $request->tugas_3 &&
$tugas->pivot->tugas_4 == $request->tugas_4 &&
$tugas->pivot->tugas_5 == $request->tugas_5 &&
$tugas->pivot->tugas_6 == $request->tugas_6 &&
$tugas->pivot->tugas_7 == $request->tugas_7 &&
$tugas->pivot->tugas_8 == $request->tugas_8 &&
$tugas->pivot->tugas_9 == $request->tugas_9 &&
$tugas->pivot->tugas_10 == $request->tugas_10){
$query = $peserta->semestertugas()->sync($id_semester,
[
'tugas_1' => $tugas->pivot->tugas_1 = $request->tugas_1,
'tugas_2' => $tugas->pivot->tugas_2 = $request->tugas_2,
'tugas_3' => $tugas->pivot->tugas_3 = $request->tugas_3,
'tugas_4' => $tugas->pivot->tugas_4 = $request->tugas_4,
'tugas_5' => $tugas->pivot->tugas_5 = $request->tugas_5,
'tugas_6' => $tugas->pivot->tugas_6 = $request->tugas_6,
'tugas_7' => $tugas->pivot->tugas_7 = $request->tugas_7,
'tugas_8' => $tugas->pivot->tugas_8 = $request->tugas_8,
'tugas_9' => $tugas->pivot->tugas_9 = $request->tugas_9,
'tugas_10' => $tugas->pivot->tugas_10 = $request->tugas_10
]);
if ($query){
return back()->with('pesan', 'Tugas peserta berhasil diedit');
}
}
else{
$query = $peserta->semestertugas()->updateExistingPivot($id_semester,
[
'tugas_1' => $tugas->pivot->tugas_1 = $request->tugas_1,
'tugas_2' => $tugas->pivot->tugas_2 = $request->tugas_2,
'tugas_3' => $tugas->pivot->tugas_3 = $request->tugas_3,
'tugas_4' => $tugas->pivot->tugas_4 = $request->tugas_4,
'tugas_5' => $tugas->pivot->tugas_5 = $request->tugas_5,
'tugas_6' => $tugas->pivot->tugas_6 = $request->tugas_6,
'tugas_7' => $tugas->pivot->tugas_7 = $request->tugas_7,
'tugas_8' => $tugas->pivot->tugas_8 = $request->tugas_8,
'tugas_9' => $tugas->pivot->tugas_9 = $request->tugas_9,
'tugas_10' => $tugas->pivot->tugas_10 = $request->tugas_10
]);
if ($query){
return back()->with('pesan', 'Tugas peserta berhasil diedit');
}
}
}
this is the edit form view
this is the table view
this is the pivot table
I hope you can understand my explanation!
There is no need to update if there is no changes. Maybe what you're after is the sync whithout detaching
public function edit(Request $request, $id_angkatan, $id_semester, $id)
{
$peserta = Peserta::where('id', $id)->first();
$query = $peserta->semestertugas()->syncWithoutDetaching([$id_semester => [
'tugas_1' => $request->tugas_1,
'tugas_2' => $request->tugas_2,
'tugas_3' => $request->tugas_3,
'tugas_4' => $request->tugas_4,
'tugas_5' => $request->tugas_5,
'tugas_6' => $request->tugas_6,
'tugas_7' => $request->tugas_7,
'tugas_8' => $request->tugas_8,
'tugas_9' => $request->tugas_9,
'tugas_10' => $request->tugas_10,
]]);
if ($query) {
return back()->with('pesan', 'Tugas peserta berhasil diedit');
}
}
$purchase_line_datas = PurchaseLine::where('transaction_id',
$transaction->id)->get(); $i = 0;
$input = [];
foreach ($purchase_line_datas as $key => $purchase_line_data) {
if (!$enable_stock_transfer) {
$qty_available = $this->productUtil->num_uf($purchases[$i]['quantity']);
} else {
$qty_available = 0;
}
$item = array(
"business_id" => $business_id,
"transaction_id" => $purchase_line_data->transaction_id,
"purchase_line_id" => $purchase_line_data->id,
"variation_id" => $purchase_line_data->variation_id,
"contact_id" => $transaction_data['contact_id'],
"product_id" => $purchase_line_data->product_id,
"ref_no" => $ref_no,
"new_barcode" => $purchase_line_data->barcode,
"default_sell_price" => $this->productUtil->num_uf($purchases[$i]['default_sell_price']),
"purchase_qty" => $this->productUtil->num_uf($purchases[$i]['quantity']),
"qty_available" => $this->productUtil->num_uf($qty_available),
"created_at" => Carbon::now()
);
array_push($input, $item);
$i++;
}
ProductPurchaseSl::insert($input);
in this code I am inserting an array but after inserting.. it's showing that one row is missing.And it's happening very often.I couldn't be able to solve this problem.
I have a form that accepts delivery of products which I noticed if I enter 0 in the quantity field it doesn't save in the database even if I add data in the calendar or in Notes field.
I already commented out the \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,iin kernel.php still doesn't work.
how can I forced laravel to save my data even if I want to put 0 in quantity? thanks in advance!
update
public function store(Request $request)
{
$input = $request->all();
$items = [];
for ($i = 0; $i <= count($input['order_id']); $i++) {
if (empty($input['stock_in_qty'][$i]) || !is_numeric($input['stock_in_qty'][$i])) continue;
$acceptItem = [
'order_id' => $input['order_id'][$i],
'product_id' => $input['product_id'][$i],
'order_item_id' => $input['order_item_id'][$i],
'delivery_date' => $input['delivery_date'][$i],
'company_id' => $input['company_id'][$i],
// 'stock_in_qty' => intval($input['stock_in_qty'])[$i],
'stock_in_qty' => $input['stock_in_qty'][$i],
// 'stock_out_qty' => $input['stock_out_qty'][$i],
// 'transfer_to' => $input['transfer_to'][$i],
'delivery_note' => $input['delivery_note'][$i],
'user_id' => $input['user_id'][$i],
];
array_push($items, Warehouse1stocks::create($acceptItem));
$stockSummary = Warehouse1StockSummaries::firstOrCreate(
['product_id' => $input['product_id'][$i]],
['qty_in' => $input['stock_in_qty'][$i],
'qty_out' => null,
]);
if (!$stockSummary->wasRecentlyCreated) {
$stockSummary->increment('qty_in', $input['stock_in_qty'][$i]);
}
}
if ($input['rd'] == $input['stock_in_qty'] || $input['rd'] == 0) {
$order_idAcceptedItem = $acceptItem['order_id'];
$setStatus = \App\Orders::where('id', '=', $order_idAcceptedItem)->first();
if ($setStatus) {
$setStatus->status_id = 4;
}
$setStatus->save();
} else {
$order_idAcceptedItem = $acceptItem['order_id'];
$setStatus = \App\Orders::where('id', '=', $order_idAcceptedItem)->first();
if ($setStatus) {
$setStatus->status_id = 3;
}
$setStatus->save();
}
return redirect()->route('orders.index');
}
empty() will return true with 0 or '0' which will mean that if you try to change the quantity to 0 the for loop will just continue on to the next loop. If you need to check if the value exists you can instead use isset().
Changing your first if statement to the following should be all you need:
if(!isset($input['stock_in_qty'][$i]) || !is_numeric($input['stock_in_qty'][$i])) continue;
Suppose I have items in database which is stored from an Excel file. All the items should be below the header of the months. I have also stored months from the file in the database. So, I want those months to be the header of those items and it's related records. In simple words, I want the header to be dynamic. This is what I have done.
I have tried many code scripts but nothing works. Like Laravel, Excel etc. Can anyone suggest me a good approach?
public function test(){
$data = Item::where('category_id',7)->get()->toArray();
$data2 = month::all();
$itemsArray[] = ['Category Id','Item Name','Created At','Updated At'];
foreach ($data as $value) {
// dd($value);
$itemsArray[] = array(
'Category Id' => $value['category_id'],
'Item Name' => $value['name'],
'Created At' => $value['created_at'],
'Updated At' => $value['updated_at'],
);
}
// Generate and return the spreadsheet
Excel::create('Items', function($excel) use ($itemsArray) {
// Set the spreadsheet title, creator, and description
$excel->setTitle('Items');
// Build the spreadsheet, passing in the items array
$excel->sheet('Items', function($sheet) use ($itemsArray) {
$cellRange = 'A1:D1';
// $spreadsheet->getActiveSheet()->getStyle('A1:D4')
// ->getAlignment()->setWrapText(true);
$sheet->getStyle($cellRange)->getFont()->setBold( true );
$sheet->getStyle($cellRange)->getFont()->setSize( '15' );
$sheet->setBorder($cellRange, 'thick' );
$sheet->getStyle($cellRange)->applyFromArray(array(
'fill' => array(
// 'type' => PHPExcel_Style_Fill::FILL_SOLID,
'color' => array('rgb' => 'A5D9FF')
)
));
$sheet->fromArray($itemsArray, null, 'A1', false, false);
});
$excel->setCreator('Laravel')->setCompany('Dev505');
$excel->setDescription('Items file');
})->download('xlsx');
}
I need help for getting the actual result.
Akhtar i suggest use to kindly install the Carbon package
https://carbon.nesbot.com/docs/
Try by updating the below code.
$data = Item::where('category_id',7)->get(); // removed toArray()
$data2 = month::all();
$itemsArray[] = ['Category Id','Item Name','Created At','Updated At'];
foreach ($data as $key=>$value) {
$itemsArray[] = array(
'month' => Carbon::now()->addMonth($key)->format('m-Y');
'Category Id' => $value['category_id'],
'Item Name' => $value['name'],
'Created At' => $value['created_at'],
'Updated At' => $value['updated_at'],
);
}
This is the actual code which I have used for excel file. I have solved my problem. Thanks and yeah I am posting this code, if anyone can get help from it.
public function export(){
$data = Category::all();
foreach ($data as $value) {
$value['items'] = Item::where('category_id',$value['id'])->get();
foreach ($value['items'] as $vl) {
$vl['record'] = Record::where('item_id',$vl['id'])->get();
}
}
$data2 = month::pluck('id','month');
foreach ($data2 as $key => $value) {
$m[] = $key;
}
array_unshift($m, 'Categories'); //Insert new element at the start of array
array_push($m, 'Total');
$itemsArray[] = $m;
foreach ($data as $value) {
$itemsArray[] = array(
$itemsArray[0][0] => $value['name'],
// $itemsArray[0][13] => 'Total',
);
foreach ($value['items'] as $val) {
$records_array = [];
$i = 0;
foreach ($val['record'] as $val5) {
$recordval = $val5['value'];
$records_array[$i] = $val5['value'];
$i++;
}
$itemsArray[] = array(
$itemsArray[0][0] => $val['name'],
$itemsArray[0][1] => $records_array[0],
$itemsArray[0][2] => $records_array[1],
$itemsArray[0][3] => $records_array[2],
$itemsArray[0][4] => $records_array[3],
$itemsArray[0][5] => $records_array[4],
$itemsArray[0][6] => $records_array[5],
$itemsArray[0][7] => $records_array[6],
$itemsArray[0][8] => $records_array[7],
$itemsArray[0][9] => $records_array[8],
$itemsArray[0][10] => $records_array[9],
$itemsArray[0][11] => $records_array[10],
$itemsArray[0][12] => $records_array[11],
// $itemsArray[0][13] => 'Total',
);
}
}
// Generate and return the spreadsheet
Excel::create('Items', function($excel) use ($itemsArray) {
// Set the spreadsheet title, creator, and description
$excel->setTitle('Items');
// Build the spreadsheet, passing in the items array
$excel->sheet('Items', function($sheet) use ($itemsArray) {
$cellRange = 'A1:M1';
$sheet->getStyle($cellRange)->getFont()->setBold( true );
$sheet->getStyle($cellRange)->getFont()->setSize( '12' );
$sheet->setBorder($cellRange, 'thin' );
$sheet->getStyle($cellRange)->applyFromArray(array(
'fill' => array(
// 'type' => PHPExcel_Style_Fill::FILL_SOLID,
'color' => array('rgb' => 'A5D9FF')
)
));
$sheet->fromArray($itemsArray, null, 'A1', false, false);
});
$excel->setCreator('Laravel')->setCompany('Dev505');
$excel->setDescription('Items file');
})->download('xlsx');
}
I have a problem where my database column is set to NULL
`max_vol` MEDIUMINT UNSIGNED NULL,
and my $_POST['max_vol[]'] is an empty string ('') right up to the point of inserting or updating the row in the database (tested output with print_r()). It then inserts 0 (zero) instead of NULL.
If I explicitly set max_vol to NULL it then works.
$value['max_vol'] = empty($value['max_vol']) ? NULL : $value['max_vol'];
but why does this happen? I thought setting an empty string to MySQL (with NULL set) inserted NULL. Here is my original code. Is this something CodeIgniter's query builder changes?
$position_form_data = array(); // positions form data store
// process the form data into arrays for database operations
foreach( $_POST as $post_key=>$post_value ) {
// ignore non-array post variables
if( is_array( $post_value ) ) {
foreach( $post_value as $form_key=>$form_value ) {
if (!isset($position_form_data[$form_key])) {
$position_form_data[$form_key] = array();
}
$position_form_data[$form_key][$post_key] = $form_value;
}
}
}
// if id exists db->update else db->insert
foreach($position_form_data as $value){
// $value['max_vol'] = empty($value['max_vol']) ? NULL : $value['max_vol'];
// data for insert and replace db operations
$data = array(
'id' => $value['id'],
'day' => $_POST['day'],
'title' => $value['title'],
'description' => $value['description'],
'max_vol' => $value['max_vol'],
'is_draft' => $is_draft,
'project_id' => $_POST['project_id']
);
//print_r($data);exit();
if( empty($value['id']) ) {
$this->db->insert('positions', $data);
} else {
$this->db->replace('positions', $data);
}
Thanks.
I can't see any mistake, I can only recommend you try this:
if( ! isset($value['max_vol'])){
if( is_null($value['max_vol'])){
$_max_vol = NULL;
}
else{
$_max_vol = NULL
}
}
else{
$_max_vol = $value['max_vol'];
}
$data = array(
'id' => $value['id'],
'day' => $_POST['day'],
'title' => $value['title'],
'description' => $value['description'],
'max_vol' => $_max_vol,
'is_draft' => $is_draft,
'project_id' => $_POST['project_id']
);