saving number field with a value of zero in Laravel 5.5 - laravel

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;

Related

Laravel - How to update table multiple rows at once

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);
}

Saving multiple model using array

Basically, I have an array of $course_id. I want to save multiple times according to number of $course_id
Here is my code:
$courselist[] = new Courselist;
for($i=0;$i<count($course_id);$i++)
{
$comparelist = Courselist::select('course_id','internal','external')
->where('course_id','=',$course_id[$i])
->get();
if($comparelist->isEmpty())
{
$courselist[$i]->course_id = $course_id[$i];
$courselist[$i]->internal = $internal;
$courselist[$i]->external = $external;
$courselist[$i]->save();
}
}
Does anyone know how can I save this?
you can use updateOrCreate() function like this
for ($i = 0; $i < count($course_id); $i++) {
Courselist::updateOrCreate([
['course_id' => $course_id[$i]],
[
'internal' => $internal,
'external' => $external
],
]);
}
ref link https://laravel.com/docs/8.x/eloquent#upserts
I use insert() function and it works
for($i=0;$i<count($course_id);$i++)
{
$data[] = array(
'course_id' => $course_id[$i],
'internal' => $internal,
'external' => $external
);
}
Courselist::insert($data);

Best way to bulk create models within Laravel?

I am creating a feature which involves creating voucher codes for discounts. I contain an input which the user can fill out with the specific amount they want.
When it hits the store method say if the user has entered 15 vouchers how would you create that many model records in one go?
From looking at the laravel documentation the only thing i've seen is using factories so i've attempted this below.
public function storeCodes(Request $request, VoucherGroup $voucherGroup)
{
VoucherCode::create([
'code' => $this->generateUniqueCode(),
'group_id' => $voucherGroup->id,
'used' => 0
]);
session()->flash('success', 'successfully created codes.');
return back();
}
private function generateUniqueCode()
{
$characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersNumber = strlen($characters);
$code = '';
while (strlen($code) < 6) {
$position = rand(0, $charactersNumber - 1);
$character = $characters[$position];
$code = $code . $character;
}
if (VoucherCode::where('code', $code)->exists()) {
dd('lol');
$this->generateUniqueCode();
}
return $code;
}
Input
<x-inputs.input size="6" type="number" name="voucher_amount" required>How many Voucher codes do you want
to
generate?
</x-inputs.input>
The problem is it creates the models but the code returns the same code for each model.
How would I create 15 models in one call with unique code fields?
The for loop below solved this for me. It was originally posted by someone but its been deleted recently so ill answer the question below but thanks to whoever provided it!
$newVouchers = [];
for ($i = 0; $i < $request->voucher_amount; $i++) {
$newVouchers[] = VoucherCode::create([
'code' => $this->generateUniqueCode(),
'group_id' => $voucherGroup->id,
'used' => 0
]);
}
You can try it shortly
do {
//generate unique code
$uniqueCode = strtoupper(substr(base_convert(sha1(uniqid(mt_rand())), 16, 36), 0, 15));
} while (VoucherCode::where('code', $uniqueCode)->exists());
VoucherCode::create([
'code' => $uniqueCode,
'group_id' => $voucherGroup->id,
'used' => 0
]);
session()->flash('success', 'successfully created codes.');
return back();
You can use Insert Statements to bulk create models
$vouchers = [];
for ($i = 0; $i < $numberOfVoucher; $i++) {
$vouchers[] = [
'code' => $this->generateUniqueCode(),
'group_id' => $voucherGroup->id,
'used' => 0
];
}
DB::table('users')->insert($vouchers);

Laravel 8 update pivot table if nothing was edited in the form

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');
}
}

Inserting and updating records from 3 tables in laravel

I am storing records for my product transfer app using 3 tables in single action. Transferhistories, Warehouse1StockSummaries and Warehouse2StockSummaries.
storing records to trasnferinghistories is ok, and also the increment method I declare to Warehouse2StockSummaries is also working fine except for Warehouse1StockSummaries.
here's my store function,
public function store(Request $request)
{
$input = $request->all();
$items = [];
for($i=0; $i<= count($input['product_id']); $i++) {
// if(empty($input['stock_in_qty'][$i]) || !is_numeric($input['stock_in_qty'][$i])) continue;
if(!isset($input['qty_in'][$i]) || !is_numeric($input['qty_in'][$i])) continue;
$acceptItem = [
'product_id' => $input['product_id'][$i],
'transfer_qty' => $input['qty_out'][$i],
'user_id' => $input['user_id'][$i]
];
array_push($items, Transferhistories::create($acceptItem));
// dd($input);
//update warehouse 1 summary
$warehouse1summary = Warehouse1StockSummaries::firstOrCreate(
['product_id' => $input['product_id'][$i]],
['qty_in' => $input['qty_in'][$i],
'qty_out' => $input['qty_out'][$i]
]);
if (!$warehouse1summary->wasRecentlyCreated) {
$warehouse1summary->increment('qty_out', $input['qty_out'][$i]);
}
//update warehouse 2 summary
$stock2Summary = Warehouse2StockSummaries::firstOrCreate(
['product_id' => $input['product_id'][$i]],
['qty_in' => $input['qty_out'][$i],'qty_out' => null]);
if (!$stock2Summary->wasRecentlyCreated) {
$stock2Summary->increment('qty_in', $input['qty_in'][$i]);
}
}
return redirect()->route('transferHistory.index');
}
updating warehouse 1 summary is not doing what it should be.
any suggestion master? thank you so much in advance!
According to laravel, firstOrCreate does not save the value, so after you do:
$warehouse1summary = Warehouse1StockSummaries::updateOrCreate(
['product_id' => $input['product_id'][$i]],
['qty_in' => $input['qty_in'][$i],
'qty_out' => $input['qty_out'][$i]
]);
Edit
The method firstOrNew will return the first or a instance of the Model.
So what you wanna do is this:
$warehouse1summary = Warehouse1StockSummaries::firstOrNew(
['product_id' => $input['product_id'][$i]],
['qty_in' => $input['qty_in'][$i],
'qty_out' => $input['qty_out'][$i]
]);
if(isset($warehouse1summary->created_at)){
$warehouse1summary->qty_out = $warehouse1summary->qty_out + $input['qty_out'][$i];
}
$warehouse1summary->save();

Resources