Laravel modify array request - arrays

im new using laravel and i get a problem.
and there is my var_dump (post request)
+request: Symfony\Component\HttpFoundation\ParameterBag {#44 ▼
#parameters: array:12 [▼
"_token" => "R1fnZMtNA55fy1plkTuztuhMZ1ExvkSw01GJq4rN"
"spg" => "test"
"nama" => "Reynaldo"
"alamat" => "123"
"hp" => "2134"
"ccs" => "1"
"cos" => "2"
"oss" => "Qty"
"tvs" => "Qty"
"tcs" => "Qty"
"lngs" => "Qty"
"tks" => "Qty"
]
this if my HTML form:
#foreach ($products as $product)
<div class="form-group row col-md-12">
<label for="{{ $product->slug }}" class="col-sm-3 col-form-label">{{ $product->nama }}</label>
<div class="col-sm-7">
<select class="custom-select col-sm-2" name="{{ $product->slug }}" required>
<option hidden>Qty</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</div>
</div>
#endforeach
the problem is, how can I modify post request (add array and don't send 'qty' value)?
i want to look like this :
+request: Symfony\Component\HttpFoundation\ParameterBag {#44 ▼
#parameters: array:12 [▼
"_token" => "R1fnZMtNA55fy1plkTuztuhMZ1ExvkSw01GJq4rN"
"spg" => "test"
"nama" => "Reynaldo"
"alamat" => "123"
"hp" => "2134"
product array [
"ccs" => "1"
"cos" => "2"
"oss" => "Qty"
"tvs" => "Qty"
"tcs" => "Qty"
"lngs" => "Qty"
"tks" => "Qty"
]
]
very grateful if someone helps. sorry for my broken English.
thankyou.

try this
<select class="custom-select col-sm-2" name="{{ $product->slug }}[]" required>
now your select data come as array formate and its name will be $product->slug's name

Related

How can I fetch the elements of a nested JSON Array in Laravel?

array:249 [▼
0 => array:6 [▼
"name" => "Afghanistan"
"state_name" => "Islamic Emirate of Afghanistan"
"capital" => array:1 [▼
0 => array:5 [
0: {5 items
"name":"Kabul"
"notes":"official"
"latitude":"34.575503"
"longitude":"69.240073"
"population":"3140853"
}]
]
"iso_3166" => array:4 [▼
"alpha2" => "AF"
"alpha3" => "AFG"
"numeric" => "004"
"subdivision" => "ISO 3166-2:AF"
]
"un_geoscheme" => array:2 [▼
"region" => "Asia"
"subregion" => "Southern Asia"
]
"population" => array:3 [▼
"density_km" => "50.38"
"total" => "32890171"
"density_mi" => "130.48"
]
]
1 => array:6 [▶]
2 => array:6 [▶]
I have been able to fetch the first-step arrays by looping through in blade file, however, I am having a little challenge in extracting the nested "capital" array in the above. My blade file code is below...
#foreach ($countriesData as $country)
<div class="p-2">
<div class="flex items-center">
<div class="ml-4 text-lg leading-7 font-semibold"><p>Country: <strong>{{ $country['name'] }}</strong></p></div>
</div>
<div class="ml-12">
<div class="mt-2 dark:text-gray-400 text-sm">
<p>Full name: <strong>{{$country['state_name']}}</strong></p>
<p>Continent: <strong>{{$country['un_geoscheme']['region']}}</strong></p>
#foreach ($country as $countryCapital)
{{-- <p>Capital: <strong>{{$countryCapital['capital']['name']}}</strong></p>
#endforeach
{{-- Do I foreach again as I have done above, use a for loop or what must I do in this particular scenario? Thanks --}}
</div>
</div>
</div>
#endforeach
So far I have been getting errors like "Undefined array key", "Cannot call string on a string", etc. I would be very glad if someone can kindly help me out with this. Also, I have done the necessary Google search but all was to no avail. Thanks.
Data source: RapidApi Countries API Documentation
Try like this $countryCapital['name']
$country['capital'] is array, iterate on that like that:
#foreach ($country['capital'] as $countryCapital)
<p>Capital: <strong>{{$countryCapital['name']}}</strong></p>
#endforeach

How to save multiple table rows in databse using Laravel

I want to be able to store multiple array values in one save operation.
Is this possible?
I have a dynamic table that produces the following array:
array:5 [▼
"_token" => "cNvFpF7e7fVl0vDxPcCOlbH0eUadXIWaokwH7z3F"
5 => array:9 [▼
0 => "1"
1 => "1"
2 => "0"
3 => "2"
4 => "0"
5 => "3"
6 => "0"
7 => "0"
8 => "0"
]
4 => array:9 [▼
0 => "1"
1 => "0"
2 => "0"
3 => "0"
4 => "0"
5 => "0"
6 => "0"
7 => "0"
8 => "0"
]
6 => array:9 [▶]
"save" => null
]
where 4 =>, 5 =>, and 6 => represent user_ids and the arrays, their respective scores.
I am not sure how I would go about iterating through the multiple arrays to store the data.
The DB table structure is id, user_id, game_id, col-1, col-2, etc
Hopefully, this is clear enough but if yo need more info, please let me know.
Thanks
Here is the table code:
This is the code I have for my table :
#foreach($players as $p)
<tr class="">
<td class="h2 align-middle">
{{ $players->get($loop->index)->first_name }}
<input type="text" name="user_id" value="{{ $p->id }}">
</td>
#foreach($innings as $inning)
<td class="p-1">
<div class="form-group_{{$p->id}} input-group p-1 h4">
<select class="custom-select custom-select-lg py-0 prc" id="" name="player[{{$p->id}}]">
<option class="" value="0">0</option>
#foreach($scores as $score)
<option name="score[]" value="{{ $score }}">{{ $score }}</option>
#endforeach
</select>
</div>
</td>
#endforeach
<td>
<div class="h1 text text-center font-weight-bold pt-2">
<output id="result_{{$p->id}}"></output>
</div>
</td>
</tr>
#endforeach
$data = [
['user_id'=>'1', 'game_id'=> 1, 'col-1'=>'some value', 'col-2'=>'other value'],
['user_id'=>'2', 'game_id'=> 1, 'col-1'=>'some new value', 'col-2'=>'other new value'],
//...
];
Eloquent approach
Model::insert($data);
Query Builder approach
DB::table('table')->insert($data);
Using the above method you can insert multiple entries in one query.

How do I instert multiple array into mysql using laravel

I want to insert data from my form into mysql bu I am getting errors. How do I solve this problem.
#foreach($assignments as $item)
<input type="text" name="class[]" value="{{$item->class}}" id="">
<input type="text" name="year[]" value="{{$item->year}}" id="">
<input type="text" name="subject_id[]" value="{{$item->subject_id}}" id="">
<label for="" class="ml-3 mt-2 mb-1">Score</label>
<input type="text" name="score[]" id="" style="width: 20rem;" class="form-control ml-3">
</div>
$class = $request->get("class");
$year = $request->get("year");
$subject_id = $request->get("subject_id");
foreach ($request->get('score') as $key => $value){
SubmitScores::create([
'class' => $key[$class],
'year' => $key[$year],
'subject_id' => $key[$subject_id]
]);
}
#endforeach
The error am getting is:
Trying to access array offset on value of type int
These are backwards:
$key[$class]
$key[$year]
$key[$subject_id]
Those should be $variable[$key]. $key is an integer index, like 0, 1, 2, and $class, $year and $subject_id ($variable) are arrays. So:
foreach ($request->get('score') as $key => $value){
SubmitScores::create([
'class' => $class[$key],
'year' => $year[$key],
'subject_id' => $subject_id[$key]
]);
}

Check for duplicate values between two arrays. If not duplicated, show label update

I would like to check for duplicate values between two arrays if not repeatedly show label update.
This is the result I want.
This is the information I sent.
"facility" => array:1 [▼
"Tour Gudie Multilingual" => array:3 [▼
0 => "Speaking Guide - English"
1 => "Audio - Thai"
2 => "Audio - Chinese"
]
]
"facility_new" => array:1 [▼
"Tour Gudie Multilingual" => array:8 [▼
0 => "Audio - Chinese"
1 => "Audio - English"
2 => "Audio - French"
3 => "Audio - German"
4 => "Audio - Indonesia"
5 => "Audio - Russia"
6 => "Audio - Thai"
7 => "Speaking Guide - English"
]
I need to check facility and facility_new if facility != facility_new
This will display an label update and text color red.
This is my code
detail.blade
<div class="box-body">
#foreach($value['facility_new'] as $fk => $fv)
#foreach($value['facility'][$fk] as $k => $v)
<div class="form-group">
<label class="col-sm-2 control-label">{{$fk}}</label>
<div class="col-sm-10">
#foreach($fv as $key => $value)
<div class="col-md-6" style="padding-bottom: 10px">
#if($v == $value)
<p>
<i class="fa fa-fw fa-check-circle text-green"></i>
<span class="text-blue">{{$value}}</span>
</p>
#else
<p style="color: red">
<i class="fa fa-fw fa-check-circle text-green"></i>
<span class="label label-danger">Update</span>
{{$value}}
</p>
#endif
</div>
#endforeach
</div>
</div>
<hr>
#endforeach
#endforeach
</div>
But I think I have two things that are solved. 1. Defragmentation. The facilities and the 2 are what I post.
A solution I can think of is to first get the unique values between the two arrays like this:
$result = array_map('unserialize',
array_diff(array_map('serialize', $facilityNew['Tour Gudie Multilingual']),
array_map('serialize', $facility['Tour Gudie Multilingual'])));
*Note that we compare the new values to the old ones.
And then in your view, you can check if the $value does not exists in the new array you can show the red/update button.
in_array($value, $result) // this should go in your #if condition

Inserting multiple records in a form

I am trying to insert several records at once from a form
This is how the form appears
<label>Drug</label>
<select class="form-inline input-sm " name="drug[]" id="drug">
#foreach($drugs as $key => $drug)
<option value="{{$drug->id}}"> {{$drug->name}}</option>
#endforeach
</select>
<input class="input-sm" type="number" class="form-control" id="amount_received" name="amount_received[]" placeholder="Amount received">
<button type="button" name="add" id="add">Add More</button>
<button type="submit" class="btn btn-primary"id="submit">Submit</button>
My controller
public function addNewStock()
{
$input = Input::all();
$insert = array();
foreach($input['drug'] as $key => $drug) {
$insert[$key]['drug'] = $drug;
}
foreach($input['amount_received'] as $item => $amount_received) {
$insert[$item]['amount_received'] = $amount_received;
}
Stock::insert($insert);
}
This is the result i get when i dd($input). This is after i submit two record, problem is it picks the last record and saves i twice in the database
array:3 [▼
"_token" => "Oz9cSvNuPmPPHKAw6uKbnmA5DIMBsxKsnubaliHt"
"drug" => array:2 [▼
0 => "1"
1 => "10"
]
"amount_received" => array:2 [▼
0 => "2"
1 => "4"
]
]
Kindly assist.

Resources