How to Save an Array data in JSON using LARAVEL Controller - database

I want to save this data generated by a create.blade.php view,
after I press submit button, the console log output is like the code below
{
"kode_trx": "PO-2019030004",
"no_po": "0005/DIN/III/2019",
"pic_po": "Carlo Donadini",
"pic_telp": "+39438778020",
"top": "RRRRR",
"supplier_id": "1",
"delivery_date": "28-03-2019",
"created_by_id": "1",
"total_barang": "7",
"grand_total": "122500000",
"tb_detail_po": [
[
"BRG-2019030004",
"Electric Deep Fryer",
"SC-81",
"290",
"440",
"310",
"4",
"17500000",
"70000000",
" ",
"4",
"PO-2019030004",
"0005/DIN/III/2019",
"1",
"5"
],
[
"BRG-2019030004",
"Electric Deep Fryer",
"SC-81",
"290",
"440",
"310",
"3",
"17500000",
"52500000",
" ",
"4",
"PO-2019030004",
"0005/DIN/III/2019",
"1",
"5"
]
]
}
so I want to save it into 2 differen table, table purchases and purchase_details
how I suppose to write my code in POController in method store? ???

You can store the data on table purchase like below
class POController extends Controller
{
public function store(Request $request)
{
$purchase = Purchase::create([
"id_penerimaan" => $request->no_nota,
"id_customer" => $request->customer,
"id_karyawan" => $request->karyawan,
"tgl_penerimaan" => \Carbon\Carbon::now()->format('Y-m-d H:i:s'),
]);
return $purchase;
}
}
And for the table purchase_details if you use Observer it would be great
Example of Observer :
class UserObserver
{
public function created(Purchase $purchase)
{
if (request()->tb_detail_po) {
foreach(request()->tb_detail_po as $detail) {
PurchaseDetail::create([
'id_purchase' => $purchase->id,
'id_penerimaan' => request()->no_nota,
'id_karyawan' => $detail[9],
'id_kategori' => $detail[8],
'sn_barang' => $detail[1],
'nama_barang' => $detail[0],
'kelengkapan' => $detail[2],
'keluhan' => $detail[3],
'id_garansi' => $detail[10],
'id_status' => '1',
'kondisi' => '1'
]);
}
}
}
}

$result = $request->all();
$created = Your Model::create([Your Data For Table 1]);
if($created) {
foreach($request->tb_detail_po as $details) {
Your Model2::create([Your Detail Data For Table 2]);
}
}

Try to use **JSON_DECODE($data)** OR **JSON_ENCODE($data)**

Related

array with objects containing array

Sorry guys if my way of asking the question in the title is not correct.
I am working on a project on react js and I am getting the data like this
[
{
"count": [
{
"1": 16
},
{
"1": 149
}
],
"day": "2019-08-27"
}
]
now this is my first time I am dealing with this kind of data and I really have no idea how can I show it like this I am really sorry guys I literally can't even show what I have tried because it does not seem relevant
[
{
count: 165
day:"2019-08-27"
}
}
Assuming the data you're getting is under a variable called data you could use reduce:
The below makes the assumption the count is always an array of objects with just 1 key called '1'.
const newData = data.map(datum => {
datum.count = datum.count.reduce((count, item) => {
return count + item['1']
}, 0)
return datum
})
You can try something like this:
let arr = [
// item
{
count: [
{
"1": 16
},
{
"1": 149
}
],
day: "2019-08-27"
}
];
arr.map(item => {
Object.keys(item).map(key => {
console.log(item[key])
// if item[key] is iterable
if(Array.isArray(item[key])) {
item[key].map(val => {
console.log(item)
})
} else {
console.log(item[key])
}
});
});
The concept is that for Objects you do a Object.keys().something and for an array you do a arr.map(item => ...)

merging nested lists and map in ruby

I have following list of maps, how can I get values inside map out and merge them as a new list
Example:
x = [ { "key1" => [{"K1" =>"123", "K2" =>"123"}] },
{ "key1" => [{"K3" =>"23", "K4" =>"32"}] },
{ "key1" => [{"K5" =>"34", "K6" =>"23"}] }]
What I want is:
[{"K1" =>"123", "K2" =>"123"},
{"K3" =>"23", "K4" =>"32"},
{"K5" =>"34", "K6" =>"23"}]
You can try the below -
x = [ { "key1" => [{"K1" =>"123", "K2" =>"123"}] },
{ "key1" => [{"K3" =>"23", "K4" =>"32"}] },
{ "key1" => [{"K5" =>"34", "K6" =>"23"}] }]
y = x.map{|h| h.map{|i,j| j} }.flatten
print(y)
This prints the below
[{"K1"=>"123", "K2"=>"123"}, {"K3"=>"23", "K4"=>"32"}, {"K5"=>"34", "K6"=>"23"}]
x.flat_map(&:entries).group_by(&:first).map{|k,v| Hash[k, v.map(&:last)]}
as:
> x = [ { "key1" => [{"K1" =>"123", "K2" =>"123"}] },
{ "key1" => [{"K3" =>"23", "K4" =>"32"}] },
{ "key1" => [{"K5" =>"34", "K6" =>"23"}] }]
> x.flat_map(&:entries).group_by(&:first).map{|k,v| Hash[k, v.map(&:last)]}
=> [{"key1"=>[[{"K1"=>"123", "K2"=>"123"}], [{"K3"=>"23", "K4"=>"32"}], [{"K5"=>"34", "K6"=>"23"}]]}]
I hope that helpful
You can simply do as below,
x.map { |z| z.values[0][0] }
# => [{"K1"=>"123", "K2"=>"123"}, {"K3"=>"23", "K4"=>"32"}, {"K5"=>"34", "K6"=>"23"}]

How to have a result array when using autocomplete in Angular 6

I want to create a array with id of products.
I have this form:
this.myform= new FormGroup({
'products_id': this.fb.array([], Validators.required),
'unit_price': new FormControl('', [Validators.required)
});
My function submit is:
onAddProducts() {
this.areWeWaiting = true;
let newp = this.myform.value
let newprod= new Products(
newp
);
this.ws.createP(newprod).subscribe(
result => {
if (result === true) {
} else {
this.areWeWaiting = false;
}
},
error => {
this.areWeWaiting = false;
}
);
}
Result from this code is this array: ["Y4FQS", "qOKTN", "sRm54"]
I want to have a result like this: ["1", "2", "3"] I want to submit Id of product, not name of product.
For this I try to change my code like this:
I declare a property prodId: Array<string>[]; and in function submit:
onAddProducts() {
this.areWeWaiting = true;
let newp = this.myform.value
newp.products_id= this.prodId;
let newprod= new Products(
newp
); ........}
This prodId I take form this function:
updateForm(ev: any, idd: any, componentid: any) {
if (ev.isUserInput) {
if (componentid === 'products_id') {
this.prodId = idd;
this.myForm['controls']['products_id'].setValue(ev.source.value);
} else {
console.log('ooops');
}
}
}
This is a function that get all product from API:
this.ws.getAllproducts().subscribe(
products=> {
this.products= products.map((prod) => {
this.filteredProducts = this.myForm.controls['products_id'].valueChanges
.pipe(
startWith(''),
map(value => this._filters(value))
);
return new Products(prod);
});
}
);
this function return:
{
"StatusCode": 0,
"StatusMessage": "OK",
"StatusDescription": [
{
"products_id": "1",
"products_name": "PkMGo",
"unit_price": 100,
},
{
"products_id": "2",
"products_name": "Y4FQS",
"unit_price": 100,
},
{
"products_id": "3",
"products_name": "Mlld0",
"unit_price": 100,
},
{
"products_id": "4",
"products_name": "q0KTN",
"unit_price": 100,
},...
]
}
This code show me only id of the latest product like "3"
Can you ask me any idea please, how to post an array like ["1", "2", "3"]
Thank you!

Howto remove duplicates in perl array of hashes based on one unique key and concatenate others

Can someone help me with a Perl code snippet to convert from:
Note: ipn must be the unique key
#part = (
{
ipn => "12345",
ticket_id => "10",
},
{
ipn => "12346",
ticket_id => "11",
},
{
ipn => "12345",
ticket_id => "12",
},
);
to:
#part_new = (
{
ipn => "12345",
ticket_id => "10, 12",
},
{
ipn => "12346",
ticket_id => "11",
},
);
Any other alternative solution to achieve something similar is also very welcome. The end result is to know which "ticket numbers" are associated with each unique part number (ipn).
Thank you.
Carl
The idea is to use a hash to keep track of which ticket_ids go with which ipns.
The keys of the subhash keyed off of the ticket_id will have all of the matching ipns. Later we can merge them together to produce a new array of hash references.
#!/usr/bin/env perl
use warnings;
use strict;
use Data::Dumper;
my #parts = (
{ ipn => "12345", ticket_id => "10", },
{ ipn => "12346", ticket_id => "11", },
{ ipn => "12345", ticket_id => "12", },
);
my %ipns;
for my $part ( #parts ) {
# Keep track of which ticket_ids go with each ipn
# 12345 -> 10
# -> 12
# 12346 -> 11
$ipns{ $part->{ipn} }->{ $part->{ticket_id} }++;
}
my #grouped_parts;
for my $ipn ( sort { $a <=> $b } keys %ipns ) {
my #tickets = sort { $a <=> $b } keys %{ $ipns{ $ipn } };
# Merge the ticket ids with each ipn
push #grouped_parts, {
ipn => $ipn,
ticket_ids => join(', ', sort { $a <=> $b } #tickets)
};
}
print Dumper(#grouped_parts);
output
$VAR1 = {
'ipn' => '12345',
'ticket_ids' => '10, 12'
};
$VAR2 = {
'ipn' => '12346',
'ticket_ids' => '11'
};

Join queries in meteor angular using publish composite

I have two Collections Category and Feeds.
Category
{
"_id": "ADFGFDF",
"title" : "title",
}
Feeds
{
"_id": "DFSAHT",
"feeds" : "ds sdsd sds",
"categoryId" : "catId"
}
I need to get the result like this:
{
"_id": "ADFGFDF",
"title" : "title",
"categoryId" : "DFSAHT"
"category" : {
"_id": "DFSAHT",
"feeds" : "ds sdsd sds",
}
}
I tried using publish-composite and here it's my code.
Server
Meteor.publishComposite('feedscateg', function () {
return {
find: function () {
return Category.find({});
},
children: [
{
find: function (cat) {
return Feeds.find({ categoryID: cat._id });
}
}
]
}
});
In client Angular i tried this:
$scope.feeds = $meteor.collection(Category).subscribe('feedscateg');
And I am confused with the view part also.
publishComposite do not modify collection data, it will load Feeds and Category separately. If you want to get category of feed item just select it from client db.
$scope.getFeedCategory = function (feedItem) {
Category.findOne({'_id': feedItem.categoryId});
};

Resources