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();
}
Related
I am getting this kind of data from the database in array form.
Illuminate\Support\Collection {#1434
#items: array:4 [
0 => 20
1 => 21
2 => 22
3 => 19
]
}
I want to insert the this array data through the controller For each array element.
foreach($plucked as $data){
$attendant_data = new EmployeeAttendant();
$attendant_data->user_id = $data;
$attendant_data->date = Carbon::now()->format('Y-m-d');
$attendant_data->time = Carbon::now()->format('H:i:s');
$attendant_data->present = '0';
$attendant_data->save();
}
Data is being inserted only for the first array element.
Just prepare the data and insert it.
$insertableAttendant = [];
foreach($plucked as $data){
$attendant_data = [];
$attendant_data['user_id'] = $data;
$attendant_data['date'] = Carbon::now()->format('Y-m-d');
$attendant_data['time'] = Carbon::now()->format('H:i:s');
$attendant_data['present'] = '0';
$insertableAttendant[] = $attendant_data;
}
EmployeeAttendant::insert($insertableAttendant);
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],
]);
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,
];
}
I am trying to search an array for an element (in this case 'electronic'), then return the nested value.
The array that I am working with
array:2 [▼
0 => array:2 [▼
"value" => "0241-6230"
"type" => "print"
]
1 => array:2 [▼
"value" => "2339-1623"
"type" => "electronic"
]
]
Below is the code I'm using.
<?php
$this->doi = 'anydoinumber';
$this->client = new Client();
$this->Url = 'https://api.crossref.org/works/:'.$this->doi;
$res = $this->client->get($this->Url);
$decoded_items = json_decode($res->getBody(), true);
if (isset($decoded_items['message']['issn-type'])) {
$this->issn = '';
} else {
// no electronic ISSN given
Log.Alert('No electronic ISSN for :'.$this->Doi);
}
The output I'm expecting
$this->issn = "2339-1623"
You can use laravel collection:
collect($array)->where('type', 'electronic')->first();
And output is:
array:2 [
"value" => "2339-1623"
"type" => "electronic"
]
You could use a simple foreach loop that adds matching elements to a results array
$filtered = [];
foreach($myarr as $i){
if($i['type'] == 'searched type')
$filtered[] = $i;
}
or you can break out of the loop when you encounter first element of given type
foreach($myarr as $i){
if($i['type'] == 'searched type')
return $i; // or $found = $i and then break;
}
PHP way:
$searchingFor = 'electronic';
$filteredArray = array_filter($initialArray, function($v, $k) use ($searchingFor) {
return $searchingFor === $v['type'];
}, ARRAY_FILTER_USE_BOTH);
//var_dump($filteredArray);
Docs.
You Have To user foreach loop
$searchterm = 'electronics';
foreach($nested as $key => $value) {
if($value['type'] == $searchterm) {
return $value['value'];
break;
}
}
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"
]