I have the following for in which I create the records
foreach ($v as $k => $f){
if($v[$k] != false && $f['zip_code'] !=''){
$state = State::whereCode($f['code'])->get();
var_dump($state[0]->id);
\App\Models\ZipCodes::create([
'uuid' => Uuid::generate(4)->string,
'zip_code' => $f['zip_code'],
'city' => $f['city'],
'county' => $f['county'],
'state_id' => $state[0]->id,
]);
}
}
I have noticed that the error appears in the field 'state_id' => $ state [0] -> id
since if I comment and delete it from the table it doesn't give me the error.
In fact if I just leave
foreach ($v as $k => $f){
if($v[$k] != false && $f['zip_code'] !=''){
$state = State::whereCode($f['code'])->get();
var_dump($state[0]->id);
}
}
The same generates the error.
Related
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);
}
this my code cause the trouble,
$cust = Customer::where('name', '=', $data[$i][0]['customer_name'])
->pluck('customer_id')[0];
this one for get customer id when i do store to sales order
$sales = array(
'customer_id' => Customer::where('name', '=', $data[$i][0]['customer_name'])->pluck('customer_id')[0],
'logistics_id' => Logistic::where('logistics_name', '=', $data[$i][0]['logistics'])->pluck('logistics_id')[0],
'subtotal' => $data[$i][0]['subtotal_rp'],
'shipping_cost' => $data[$i][0]['shipping_cost_rp'],
'discount_code' => 0,
'date_of_sales' => $data[$i][0]['date'],
'grand_total' => $data[$i][0]['grand_total_rp'],
'tax' => $data[$i][0]['tax_rp'],
'status' => $data[$i][0]['status'],
'discount_amount' => $data[$i][0]['discount_amount_rp']
);
$store_so = SalesOrder::create($sales);
but, when i do dd(), i get the right data
First of all, you need to check if the $data variable returns the data as you expect.
dd($data);
Next, you need to check that the $data array has the number of elements according to $total_data.
dd(count($data) == $total_data));
So basically, you just need to give condition or try-catch (recommended) :
if (isset($data[$i][0])) {
$customer = Customer::where('name', $data[$i][0]['customer_name'])->first();
$logistic = Logistic::where('logistics_name', $data[$i][0]['logistics'])->first();
if(!$customer){
dd('No customer found!');
}
if(!$logistic){
dd('No logistic found!');
}
$sales = [
'customer_id' => $customer->customer_id,
'logistics_id' => $logistic->logistics_id,
'subtotal' => $data[$i][0]['subtotal_rp'],
'shipping_cost' => $data[$i][0]['shipping_cost_rp'],
'discount_code' => 0,
'date_of_sales' => $data[$i][0]['date'],
'grand_total' => $data[$i][0]['grand_total_rp'],
'tax' => $data[$i][0]['tax_rp'],
'status' => $data[$i][0]['status'],
'discount_amount' => $data[$i][0]['discount_amount_rp'],
];
$store_so = SalesOrder::create($sales);
}
else{
dd('No $data[$i][0] found!');
}
PS : I recommend using the first() method instead of pluck('customer_id')[0].
It seems you need to get a customer_id from a customer_name.
Try to make everything simple:
$sales = array(
'customer_id' => Customer::where('name', $data[$i][0]['customer_name'])->first()->id,
...
);
$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 am trying to integrate coinpayment. By using this - https://github.com/hexters/CoinPayment
But I got error says:
exception: "ErrorException"
file: "C:\xampp\htdocs\coinpayment\vendor\hexters\coinpayment\src\Http\Controllers\CoinPaymentController.php"
line: 45
message: "Division by zero"
Please Refer to author new commits in the git repository as he updated the package https://github.com/hexters/CoinPayment/commit/cf7de99e18948fe385e75dfa8ded0fb378c33ad4
In case the link is not working then go to your app route and then to vendor folder and follow path vendor/hexters/coinpayment/src/Http/Controllers and update CoinPaymentController.php file's public function ajax_rates() function with the below given code
public function ajax_rates(Request $req, $usd){
$coins = [];
$aliases = [];
$rates = CoinPayment::api_call('rates', [
'accepted' => 1
])['result'];
$rateBtc = $rates['BTC']['rate_btc'];
$rateUsd = $rates[config('coinpayment.default_currency')]['rate_btc'];
$rateAmount = $rateUsd * $usd;
$fiat = [];
$coins_accept = [];
foreach($rates as $i => $coin){
if((FLOAT) $rates[$i]['rate_btc'] > 0) {
if((INT) $coin['is_fiat'] === 0){
$rate = ($rateAmount / $rates[$i]['rate_btc']);
$coins[] = [
'name' => $coin['name'],
'rate' => number_format($rate,8,'.',''),
'iso' => $i,
'icon' => 'https://www.coinpayments.net/images/coins/' . $i . '.png',
'selected' => $i == 'BTC' ? true : false,
'accepted' => $coin['accepted']
];
$aliases[$i] = $coin['name'];
}
if((INT) $coin['is_fiat'] === 0 && $coin['accepted'] == 1){
$rate = ($rateAmount / $rates[$i]['rate_btc']);
$coins_accept[] = [
'name' => $coin['name'],
'rate' => number_format($rate,8,'.',''),
'iso' => $i,
'icon' => 'https://www.coinpayments.net/images/coins/' . $i . '.png',
'selected' => $i == 'BTC' ? true : false,
'accepted' => $coin['accepted']
];
}
if((INT) $coin['is_fiat'] === 1){
$fiat[$i] = $coin;
}
}
}
return response()->json([
'coins' => $coins,
'coins_accept' => $coins_accept,
'aliases' => $aliases,
'fiats' =>$fiat
]);
}
I am getting a bit issue with my calculation, when I pass Discount and Debit values it is doing its job, but when Values of Discount and Debit is nothing it returns a blank page. Here is my Model.. CODEIGNITER.
function createInvoice() {
$this->load->helper('date');
$date = date('Y-m-d H:i:s');
$data = array(
'Date' => $date,
'Terms_Of_Payment' => $this->input->post('termsOfPayment'),
'Sub_Total' => $this->input->post('subTotal'),
'Total' => $this->input->post('total') - $this->input->post('discount'),
'Discount' => $this->input->post('discount'),
'Debit' => $this->input->post('debit'),
'Payment_Cridet' => $this->input->post('total') - $this->input->post('debit') - $this->input->post('discount'),
'Note' => $this->input->post('note'),
'Customer_ID' => $this->input->post('customerId'),
'User_ID' => $this->session->userdata('id'));
$this->db->insert('invoice', $data);
return ($this->db->affected_rows() != 1) ? false : true;
}
The best way is to use the ternary operator.
$subtotal = $this->input->post('subTotal') == "" ? 0 : $this->input->post('subTotal');
And if your php version is 7.0> then use
$subtotal = $this->input->post('subTotal') ?? 0;
Use ternary operator at data array when assinging the value of debit and discount.Code is looks like as below:
function createInvoice() {
$this->load->helper('date');
$date = date('Y-m-d H:i:s');
$data = array(
'Date' => $date,
'Terms_Of_Payment' => $this->input->post('termsOfPayment'),
'Sub_Total' => $this->input->post('subTotal'),
'Total' => $this->input->post('total') - isset($this->input->post('discount'))?$this->input->post('discount'):0,
'Discount' => isset($this->input->post('discount'))?$this->input->post('discount'):0',
'Debit' => isset($this->input->post('debit'))?$this->input->post('debit'):0,
'Payment_Cridet' => $this->input->post('total') - isset($this->input->post('debit'))?$this->input->post('debit'):0 - isset($this->input->post('discount'))?$this->input->post('discount'):0',
'Note' => $this->input->post('note'),
'Customer_ID' => $this->input->post('customerId'),
'User_ID' => $this->session->userdata('id'));
$this->db->insert('invoice', $data);
return ($this->db->affected_rows() != 1) ? false : true;
}
Ternary operator logic is the process of using (condition) ? (true return value) : (false return value) statements to shorten your if/else structures.
So, you can use ternary operator login for pass zero if input text is blank.
Now, some change in your createInvoice() function like me :
$subTotal = $this->input->post('subTotal') == "" ? 0 : $this->input->post('subTotal');
$total = $this->input->post('total') == "" ? 0 : $this->input->post('total');
$discount = $this->input->post('discount') == "" ? 0 : $this->input->post('discount');
$debit = $this->input->post('debit') == "" ? 0 : $this->input->post('debit');
$data = array(
'Date' => $date,
'Terms_Of_Payment' => $this->input->post('termsOfPayment'),
'Sub_Total' => $subTotal,
'Total' => ($total - $discount),
'Discount' => $discount,
'Debit' => $debit,
'Payment_Cridet' => $total - $debit - $discount,
'Note' => $this->input->post('note'),
'Customer_ID' => $this->input->post('customerId'),
'User_ID' => $this->session->userdata('id')
);