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.
Related
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
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
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
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.
Im using dynamic form input inserting array data but validation for array is not works for me. without input its saving in database as blank values.
In a table column i have inert my form input like this, adding dynamic form field im using vueJs:
<tr v-for="row in rows">
<td>{!! Form::text('description[]',null,['class' => 'input-field input-sm','v-model'=>'row.description']) !!}
#if ($errors->has('description'))
<span class="error"><i class="glyphicon glyphicon-warning-sign" data-toggle="tooltip" data-placement="top" title="{{ $errors->first('description') }}"></i></span>#endIf</td>
<td>{!! Form::text('log_time[]',null,['class' => 'input-field input-sm','v-model'=>'row.log_time']) !!}
#if ($errors->has('log_time'))<span class="error"><i class="glyphicon glyphicon-warning-sign" data-toggle="tooltip" data-placement="top" title="{{ $errors->first('log_time') }}"></i></span>#endIf </td>
<td> <a #click="removeRow(row)"><button class="btn btn-danger" type="button" id="dim">
<span class="glyphicon glyphicon-minus"></span></button> </a>
<a #click="addRow"><button class="btn btn-success" type="button" id="dim">
<span class="glyphicon glyphicon-plus"></span></button></a>
</td>
</tr>
My Controller store function :
protected $rules = [
'row.description' => ['required|array'],
'row.log_time' => ['required|array'],
];
public function store(PslCall $call,Request $request)
{
$this->validate($request, $this->rules);
$data = array();
foreach($request->description as $key=>$value){
$data[]=[
'description'=> $value,
'log_time'=> $request->log_time[$key],
'call_id'=>$call->id,
'created_at' => Carbon::now(),
'updated_at' => Carbon::now(),
];
}
PortLog::insert($data);
return Redirect::route('calls.logs.index',$call->id)->with('message','You have successfully submitted');
}
here you can check my dd() without input i can insert :( for this i have to give validation ;
array:2 [▼
0 => array:5 [▼
"description" => ""
"log_time" => ""
"call_id" => 2
"created_at" => Carbon {#351 ▶}
"updated_at" => Carbon {#352 ▶}
]
1 => array:5 [▼
"description" => ""
"log_time" => ""
"call_id" => 2
"created_at" => Carbon {#353 ▶}
"updated_at" => Carbon {#354 ▶}
]
]
There is issue with your $rules Try that
protected $rules = [
'description.*' => 'required|min:1',
'log_time.*' => 'required|min:1',
];