How do I update new collection values? - php

I need a way to update a collection that I am getting from the html form.
I have an array of
Product ids:
array:3 [
0 => "1"
1 => "2"
2 => "3"
]
Product quantity:
array:3 [
0 => "15"
1 => "5"
2 => "7"
]
Product Price:
array:3 [
0 => "12.00"
1 => "2.50"
2 => "4.00"
]
I am storing/saving the values to the database with something like this:
$product_ids = $request->get('product_ids');
$product_prices = $request->get('product_prices');
$product_quantities = $request->get('product_quantities');
$product_descriptions = $request->get('product_descriptions');
for ($i = 0; $i < sizeof($product_ids); $i++) {
$item = new Order_item;
$item->order_id = $order->id;
$item->product_id = $product_ids[$i];
$item->unit_price = $product_prices[$i];
$item->description = $product_descriptions[$i];
$item->quantity = $product_quantities[$i];
$item->save();
}

You can find the record based on id and then save the changed data:
$flight = App\Flight::find(1);
$flight->name = 'New Flight Name';
$flight->save();
From https://laravel.com/docs/7.x/eloquent#updates
If you're using order_id as the key, then use a where() statement with first():
$order = App\Order_item::where('order_id', 1)->first();

I fixed the issue with something like this:
$product_ids = $request->get('product_ids');
$product_prices = $request->get('product_prices');
$product_quantities = $request->get('product_quantities');
$product_descriptions = $request->get('product_descriptions');
Fetched the values using $request->get.
And updated the values using a for loop.
for ($i = 0; $i < sizeof($product_ids); $i++) {
Order_item::where('order_id', $id)
->where('product_id', $i + 1)->update(
[ 'quantity' => $product_quantities[$i],
'unit_price' => $product_prices[$i],
'description' => $product_descriptions[$i],
]);

Related

Get data from multiple array in Controller

I have country list in array with multiple array, like:
public static function listCountries()
{
$this->country = array(
array(1, 'SAD', 'sad.png'),
array(2, 'Argentina', 'argentina.png'),
array(3, 'Australija', 'australija.png'),
array(4, 'Novi Zenland', 'noviz.png'),
array(5, 'Belgija', 'belg.png'),
array(6, 'Nizozemska', 'nizozemska.png')
);
}
But when i do foreach for array, i'm getting this:
//From DB
$item->country = "1,4";
$item->country = explode(",", $item->country);
for($i=0; $i < count($item->country); $i++) {
$index = $item->country[$i];
if( !empty($this->country[$index]) ) {
$item->country[$i] = $this->country[$index];
}
}
$item->country = implode(",", $item->country);
echo $item->country;
But i'm getting something like this:
array:2 [▼
0 => array:3 [▼
0 => 5
1 => "Belgija"
2 => "belg.png"
]
1 => array:3 [▼
0 => 2
1 => "Argentina"
2 => "argentina.png"
]
]
1 = SAD, 4 = Novi Zenland, not Belgija and Argentina
There is no good country, also no data what i want. How to fix this?
You can use this foreach loop to go through the other array and swap the string if the number matches:
$item->country = "1,4";
$item->country = explode(",", $item->country);
for($i=0; $i < count($item->country); $i++) {
$index = $item->country[$i];
foreach($this->country as $c) {
if($c[0] == $index) {
$item->country[$i] = $c[1]; // or $item->country[$i] = $c; if you want all three items
break;
}
}
}
$item->country = implode(",", $item->country);
echo $item->country;
// Should output: SAD,Novi Zenland
The indexes in arrays are 0-based, which means that:
$index = $item->country[$i];
Has to become
$index = $item->country[$i - 1];
To correlate with the country ids. Otherwise, it is always one off. This is assuming that the ids are always ordered from least to greatest, and all ids are a continuous range.

how to insert a nested 2d array in database in laravel using realtionship

I am trying to save the Home model with its realation ship called Phone that I want to insert unlimited Phones for it . Now I am inserting home without any problem but when it comes to phone I can't insert my 2d array into phone ! Here is my controller :
$validated = $request->all();
if (!$validated) {
return $this->sendError('Validation Error.', $validated->errors());
}
$home = Home::create($validated);
$phones = $request->input('phones');
for ($i =0; $i < count($phones); $i++) {
$insertPhone[$i] = json_decode($phones[$i]);
}
dd($insertPhone);
$home->phones()->createMany($insertPhone);
return new HomeResource($home);
and down there is the dd result of $insertPhone :
array:2 [
0 => {#533
+"value": "123"
+"is_attachment": "true"
}
1 => {#538
+"value": "456"
+"is_attachment": "true"
}
]
createMany expects a multidimensional array with key / value, for example :
$home->phones()->createMany([
[
'number' => '049230323432',
],
[
'number' => '432094249023',
],
]);
So you should do like :
$phones = $request->input('phones');
$insertPhone = [];
foreach ($phones as $phone) {
$insertPhone []= [
'number' => $phone,
];
}

Laravel: Sum of foreach

I have this foreach loop:
my $items looks like this:
array:2 [▼
0 => array:3 [▼
"variant_id" => "393c6c70-8cb7-11e8-815a-f9c70a1fbe8e"
"name" => "Medicine 1"
"quantity" => "1"
]
1 => array:3 [▼
"variant_id" => "8a80d340-e0c1-11e8-86a4-3b06d2b50e37"
"name" => "Medicine 2"
"quantity" => "1"
]
]
Now, in my foreach, I compare the variant_id to my database and get the points from that database and multiply it by the quantity of the Medicines.
points for Medicine 1 is = 50
points for Medicine 2 is = 20
so it should be 50(medicine 1 points) multiplied by 1 (quantity of medicine 1) and
20 x 1 (medicine 2) and then the sum of both which should be equal to 70. But when I dd($sum) what I get is 50 which is only for medicine 1.
$sum = 0;
foreach ($items as $variants) {
$id = $variants['variant_id'];
$quantity = $variants['quantity'];
$find = Activity::where('variant_id', $id)->first();
$act_points = $find->points * $quantity;
$sum += $act_points;
dd($sum);
}
What am I missing here?
You can see dd() as a Dump and Die. This will dump the asked data and kills the script. Because this is executed, the script gets terminated before it can finish the loop
You need to use the dd() after the foreach-loop
$sum = 0;
foreach ($items as $variants) {
$id = $variants['variant_id'];
$quantity = $variants['quantity'];
$find = Activity::where('variant_id', $id)->first();
$act_points = $find->points * $quantity;
$sum += $act_points;
}
dd($sum);
If you do not want to halt the execution of your script, use the dump function instead.
https://laravel.com/docs/5.7/helpers#method-dd
I figure out you are doing Debug dump (dd) inside foreach loop so it will just stop after the first iteration.
You should do dd outside of foreach loop.
To rationalize and avoid multiple DB calls yu can do the next
$input = [
[
"variant_id" => "393c6c70-8cb7-11e8-815a-f9c70a1fbe8e",
"name" => "Medicine 1",
"quantity" => "1",
],
[
"variant_id" => "8a80d340-e0c1-11e8-86a4-3b06d2b50e37",
"name" => "Medicine 2",
"quantity" => "1",
],
];
$sum = Activity::whereIn('variant_id', array_column($input, 'variant_id'))->get()->sum(function ($s) use ($input) {
foreach ($input as $k => $v) {
if ($s->variant_id == $v['variant_id']) {
return $s->points * $v['quantity'];
}
}
});
This will work if in $input array there is no repeating elements by variant_id.
In other words if each $input[n]['variant_id'] value is unique.

Comparing two arrays depending on the order of id

I have this code and I want to compare depending of the order of id
this is the code:
foreach ($questions as $question) {
$question_answers = OrderingAnswer::where('question_id', $question->id)
->where('deleted', 0)
->get()
->toArray();
$question_answer = $request->except('_token', 'test_id');
$answers = $question_answer[ $question->id];
foreach ($question_answers as $answer) {
if ($answer === $answers) {
$answer_result[] = 1;
} else {
$answer_result[] = 0;
}
}
if (in_array(0, $answer_result)) {
$question_results[$question->id] = 0;
} else {
$question_results[$question->id] = 1;
}
$answer_result = [];
$results = $question_results;
}
I have tried array_diff, array_intersect but wont work, the result of the dd($answer); is like this
array:5 [▼
"id" => 239
"question_id" => 239
"text" => "something"
"order" => 1
"deleted" => 0
]
and the result of the dd($answers); is this
array:4 [▼
0 => "239"
1 => "240"
2 => "241"
3 => "242"
]

How to save it in database

In my database i have column like id,product_id,company_name,service,qty,delivery_cost,delivery_date,order_status etc.
I view i used Jquery and Html and jquery dynamically add more input fields of product_id,service,delivery_cost,qty,delivery_date,order_status on clicking ADD more button.On submiting form i got this in controller on doing dd($allData);
My question is how can i save this data in database
array:8 [▼
"_token" => "gSddIeA11OBV60xU9YiDXn8fmsfkwxlQ85QmDdkQ"
"service" => array:3 [▼
0 => "cement"
1 => "iron"
2 => "steel"
]
"qty" => array:3 [▼
0 => "5"
1 => "44"
2 => "5"
]
"delivery_cost" => array:3 [▼
0 => "5465"
1 => "553"
2 => "554"
]
"delivery_date" => array:3 [▼
0 => "2016-12-16"
1 => "2016-12-08"
2 => "2016-12-17"
]
"order_status" => "Confirm"
"delivery_vehicle" => array:1 [▼
0 => "Self_elivery"
1 => "Self_elivery"
2 => "Self_elivery"
]
]
public function store(Request $request)
{
$allData= $request->all();
dd($allData);
$product = new Order;
}
i try this
public function store(Request $request)
{
$date = $request->get('delivery_date');
$cost = $request->get('delivery_cost');
$service = $request->get('service');//add quotes next to service
foreach($date as $deliveryDate)
{
foreach($cost as $proAmount){
$db = new Order;
$db->delivery_date = $deliveryDate;
$db->amount = $proAmount;
$db->save();
}
}
return"ok";
}
I tried this way but it store same data multiple times may be because of loop inside of loop.I need your help to store this data in database
Using for() should work for you:
$data = $request->all();
for ($i = 0; $i < count($data['delivery_date']); $i++) {
$db = new Order;
$db->delivery_date = $data['delivery_date'][$i];
$db->delivery_cost = $data['delivery_cost'][$i];
....
$db->save();
}
You can try this If you want to use foreach $key will give you the index.
$date = $request->get('delivery_date');
$cost = $request->get('delivery_cost');
$service = $request->get('service');
foreach($date as $key=>$deliveryDate)
{
$db = new Order;
$db->delivery_date = $deliveryDate;
$db->amount = $cost[$key];
$db->save();
}
return"ok";
Hope this help you. Ask if any query
Do bulk insert instead of running new sql query for every insert(if all the request params exist in single table).
$data = [];
foreach ($request->all() as $param => $val) {
if( is_array($val) ) // Ignore string params. i.e. _token, order_status
{
foreach ($val as $key => $value) {
$data[$index][$param] = $value;
$data[$index]['created_at'] = \Carbon\Carbon::now();
$data[$index]['updated_at'] = \Carbon\Carbon::now();
$index++;
}
$index = 0;
}
}
Model::insert($data);
Using foreach() should work for you very easy:
$inputdata = $request->all();
foreach ($inputdata as $key=>$val) {
$dbdata = new Order;
$dbdata ->delivery_date = $data['delivery_date'][$key];
$dbdata ->delivery_cost = $data['delivery_cost'][$key];
....
$dbdata ->save();
}

Categories