For each problems Laravel Blade - php

Hi I'd like to ask if my query is correct.
#foreach ($indemnity_benefits->where(['indemnity_benefit_type' => 'Ee', 'indemnity_benefit_group' => 'Basic Benefits'])->get() as $key => $benefit)
<tr>
<td>
<input type="hidden" name="bBenefitEeDesc[]" value="{{$benefit->indemnity_benefit_desc}}">
<span class="p-2">{{$benefit->indemnity_benefit_desc}}</span>
</td>
<td><input style="width: 75px;" name="bBenefitEeMaxDays[]" value="{{old('bBenefitEeMaxDays.'.$key, $benefit->indemnity_benefit_maxdays)}}" class="form-control form-control-sm"></td>
{{-- #for ($index = 0; $index < $noOfCols; $index++)
<td><input name="bBenefitEeValue{{$key}}[]" value="{{old('bBenefitEeValue'.$key.'.'.$index, '')}}" type="text" class="form-control form-control-sm"></td>
#endfor --}}
#foreach ($indemnity_values->where('indemnity_benefit_id', $benefit->id)->get() as $item)
<td><input name="" value="{{$item->indemnity_benefit_value}}" type="text" class="form-control form-control-sm"></td>
#endforeach
</tr>
#endforeach
When I put $benefit->id in the second for each only one row with columns appear. Something like this.
Here are the queries:
$sob_indemnity = SobGhIndemnityComprehensive::where('rfq_version_id', $rfq_version_id)->where('type', 'Indemnity')->first();
//Indemnity Levels
$sob_indemnity_levels = SobghIndemnityLevel::where('sob_gh_ind_comp_id', $sob_indemnity->id);
$indemnity_benefits = SobghIndemnityBenefit::where('sob_gh_ind_comp_id', $sob_indemnity->id);
$indemnity_values = SobghIndemnityBenefitsValue::where('sob_gh_ind_comp_id', $sob_indemnity->id);

Your $indemnity_values is a mutable object, so on your first line you are calling:
$indemnity_values
->where('indemnity_benefit_id', 1)
->get()
And on your second line you are calling the following that is obviously returning an empty collection.
$indemnity_values
->where('indemnity_benefit_id', 1)
->where('indemnity_benefit_id', 2)
->get()
you need to clone your query:
#foreach ((clone $indemnity_values)->where('indemnity_benefit_id', $benefit->id)->get() as $item)

Related

Laravel/HTML - Getting the assigned ID for every <input type = "hidden"> in each table row

I am trying to pass every ID retrieved from the database by getting them from every field with a type of hidden for every table row. This is the picture of what I am currently doing:
I already assigned IDs on their respective fields but every time submit the form it always retrieves the last ID which is ID 10. For example if I clicked the one of the radio button of second row which has to be an ID of 7 it will ALWAYS retrieve the last row's ID (10) instead of its corresponding ID which is 7.
#foreach($flights_depart as $flight)
<tr>
<td>{{ $flight->flight_no }}</td>
<td>{{ $flight->departure_schedule }}</td>
<td>{{ $depart_source_country }}</td>
<td>{{ $flight->arrival_schedule }}</td>
<td>{{ $destination->country_name }}</td>
#for($y=$counter1; $y<#count($first_durations); $y++)
#php
$search = "0 Day";
$position = strpos($first_durations[$y], $search);
if($position === 0){
$currentDuration = substr($first_durations[$y], 9);
}else{
$currentDuration = $first_durations[$y];
}
#endphp
#break;
#endfor
<td> {{ $currentDuration }} </td>
<td> <!-- ECONOMY CLASS -->
#php
$total_fare1 = intval($book_details['seat_qty'] * ($booking_types[0] + $destination->country_price));
echo "<center>PHP ". number_format($total_fare1, 2, '.', ',');
echo "<br>";
#endphp
<input type="radio" name="total_departure_fare" value="{{$total_fare1}}" required>
</td>
<td> <!-- BUSINESS CLASS -->
#php
$total_fare2 = intval($book_details['seat_qty'] * ($booking_types[1] + $destination->country_price));
echo "<center>PHP ". number_format($total_fare2, 2, '.', ',');
echo "<br>";
#endphp
<input type="radio" name="total_departure_fare" value="{{$total_fare2}}" required>
</td>
<td> <!-- FIRST CLASS -->
#php
$total_fare3 = intval($book_details['seat_qty'] * ($booking_types[2] + $destination->country_price));
echo "<center>PHP ". number_format($total_fare3, 2, '.', ',');
echo "<br>";
#endphp
<input type="radio" name="total_departure_fare" value="{{$total_fare3}}" required>
</td>
<input type="hidden" value="{{$flight->flight_id}}" name="departure_flight_id"> //THIS IS FIELD THAT IS RESPONSIBLE FOR ASSIGNING IDS
</tr>
#php
$counter1++;
#endphp
#endforeach
<div class="form-group row mb-0">
<div class="col-md-6 offset-md-10">
<button type="submit" class="btn btn-primary">
{{ __('Continue') }}
</button>
</div>
</div>
I already tried placing it outside the table row but it does not change anything. How do I retrieve the right ID for each table row?

Laravel Inserting Data Only the Last List or Data

I want to insert the data specifically look at my page.
When I set time the data will be inserted. But what I have right now is the data that is inserted is the very last data on this table which is January-31-2019 how can I fix it so when I insert data, the data will insert any of the data that is list on the table.
Controller
public function insertSchedule(Request $request)
{
$employeeTimeSet = new Schedule;
$employeeTimeSet->employee_no = $request->input('hidEmployeeno');
$employeeTimeSet->last_name = $request->input('hidEmployeeLast');
$employeeTimeSet->first_name = $request->input('hidEmployeeFirst');
$employeeTimeSet->date_today = $request->input('dateToday');
$employeeTimeSet->time_in = $request->input('timeIn');
$employeeTimeSet->time_out = $request->input('timeOut');
$employeeTimeSet->save();
$notification = array(
'message' => 'Click PLAN TIME! Employee Time Set!',
'alert-type' => 'success'
);
return redirect()->back()->with($notification, 'Employee Time Set');
}
View
{!! Form::open(['action' => 'Admin\EmployeeFilemController#insertSchedule', 'method' => 'POST']) !!}
<div class="row">
<div class="form-group col-md-12">
<small>Employee No. and Name:</small>
<b><i> {{ $employee->employee_no }} : {{ $employee->last_name }}, {{ $employee->first_name }}</i></b>
<input type="hidden" name="hidEmployeeno" value='<?php echo $employee->employee_no ?>'>
<input type="hidden" name="hidEmployeeLast" value='<?php echo $employee->last_name ?>'>
<input type="hidden" name="hidEmployeeFirst" value='<?php echo $employee->first_name ?>'>
<hr>
</div>
</div>
#php
$today = today();
$dates = [];
for($i=1; $i < $today->daysInMonth + 1; ++$i) {
$dates[] = \Carbon\Carbon::createFromDate($today->year, $today->month, $i)->format('F-d-Y');
}
#endphp
<table class="table">
<thead>
<tr>
<th>DATE TODAY</th>
<th>TIME IN</th>
<th>TIME OUT</th>
<th>ACTION</th>
</tr>
</thead>
<tbody>
#foreach($dates as $date)
<tr>
<td><b>{{ $date }}</b></td>
<input type="hidden" name="dateToday" value="{{ $date }}">
<td><input type="time" name="timeIn" class="form-control col-md-10"></td>
<td><input type="time" name="timeOut" class="form-control col-md-10"></td>
<td> {{Form::button('<i class="fa fa-clock"> SET TIME</i>',['type' => 'submit','class' => 'btn btn-warning btn-sm', 'style'=>"display: inline-block;"])}}</td>
</tr>
#endforeach
</tbody>
</table>
{!! Form::close() !!}
The issue is that you are using one form with multiple inputs in it and there is no unique name to them. If you inspect the code, your form will have 31 inputs with name timeIn and 31 inputs with name timeOut.
When you submit the form, It picks the last timeIn and timeOut.
Two soluctions
Make multiple forms (put form tags in while loop, it will create 31 forms in the page)
Use ajax to send data to the server.
You need to use [] for your textbox names. Example : name[] and you need to use a loop to post it. Example - how to get post from same name textboxes in php
In Addition to the #Swaroop's Answer there is another option. You can make array of these fields in the same form and submit them. and at the server end, you will have to loop through posted array so that you can process them further.

Why is my form only passing input data from last field within foreach loop to database?

I am trying to update some data in my database from a form with this foreach loop:
$players_ids = $_POST['player_ids'];
foreach($players_ids as $player_id) {
foreach($_POST['goals'] as $goals) {
var_dump($goals);
$updateGoals = \DB::table('stats')->where('id', $player_id)->update(['goals' => $goals]);
}
foreach($_POST['assists'] as $assists) {
var_dump($assists);
$updateAssists = \DB::table('stats')->where('id', $player_id)->update(['assists' => $assists]);
}
foreach($_POST['tackles'] as $tackles) {
var_dump($tackles);
$updateTackles = \DB::table('stats')->where('id', $player_id)->update(['tackles' => $tackles]);
}
}
Here is the form:
#foreach($players as $player)
<tr>
<input type="hidden" name="player_ids[]" value="{{ $player->id }}">
<input type="hidden" name="game_id" value="{{ $game->id }}">
<td>{{ $player->fn }} {{ $player->ln }}</td>
<td><input type="number" value="0" name="goals[]" placeholder="goals"></td>
<td><input type="number" value="0" name="assists[]" placeholder="assists"></td>
<td><input type="number" value="0" name="tackles[]" placeholder="tackles"></td>
</tr>
#endforeach
When I try to submit this data, instead of passing through the individual stats of each player to the database, it only passes through the data of the last player in the foreach loop to every player. So if the final player has 3 goals, everyone will get 3 goals in the database. This is obviously not how I would like this to function.
I know soon after this I will run into an issue with updating the data in my database, because I'm not looking to override what is already in the database, but instead add the value passed through to the value already in the database, so if anyone has any knowledge on that as well would be greatly appreciated.
Your form has multiple copies of elements with the same name. Due to this, it only uses the last element with that name.
To utilize an array of values, you will need to use [] like you did for player_ids.
The reason this is happening is you are updating each player with the very last value set for goals, assists, and tackles. (Actually you are updating each player with every set for goals, assists and tackles, resulting in many queries.) You might need to change up your inputs so you know exactly what to update each player with.
This will also result in less looping and fewer queries.
#foreach($players as $player)
<tr>
<input type="hidden" name="player_ids[]" value="{{ $player->id }}">
<input type="hidden" name="game_id" value="{{ $game->id }}">
<td>{{ $player->fn }} {{ $player->ln }}</td>
<td><input type="number" value="0" name="goals[{{ $player->id }}]" placeholder="goals"></td>
<td><input type="number" value="0" name="assists[{{ $player->id }}]" placeholder="assists"></td>
<td><input type="number" value="0" name="tackles[{{ $player->id }}]" placeholder="tackles"></td>
</tr>
#endforeach
$players_ids = $_POST['player_ids'];
foreach ($players_ids as $player_id) {
$updates = \DB::table('stats')->where('id', $player_id)->update([
'goals' => $_POST['goals'][$player_id],
'assists' => $_POST['assists'][$player_id],
'tackles' => $_POST['tackles'][$player_id]
]);
}
This is untested, but if you want it to be additive in your database and not overwrite past values, you might be able to do something like this...
$players_ids = $_POST['player_ids'];
foreach($players_ids as $player_id) {
$updates = \DB::table('stats')->where('id', $player_id)->update([
'goals' => $_POST['goals'][$player_id] . '+' . DB::raw('goals'),
'assists' => $_POST['assists'][$player_id] . '+' . DB::raw('assists'),
'tackles' => $_POST['tackles'][$player_id] . '+' . DB::raw('tackles')
]);
}
Generally a better route would be to not make them additive though, and then you'd just use sum() to figure out how many goals, tackles, and assists each player has. Then you'd be able to do additional filtering on that like goals by date, goals by game, etc...

Output Laravel collection in groups of two

I'm struggling to split the table into two columns in Laravel blade. For example if I have 17 rows, I want to display 9 of them in first column, and the rest in the second. This is my code:
#foreach($idAndProducts as $id)
<tr>
<td width="15px">
<input type="checkbox" id="products{{$id->product_id}}" name="products[]" value="{{$id->product_id}}">
<input type="hidden" name="campaignID[]" value="{{$id->id}}"></td>
<td width="480px"><label for="products{{$id->product_id}}">{{$id->products}}</label></td>
</tr>
#endforeach
Is there a way to do this within a Blade or using jQuery?
You can use array_chunk to split them into groups of two. Try this:
#foreach ( array_chunk($idAndProducts, 2) as $row )
<tr>
#foreach ( $row as $id )
<td width="15px">
<input type="checkbox" id="products{{$id->product_id}}" name="products[]" value="{{$id->product_id}}">
<input type="hidden" name="campaignID[]" value="{{$id->id}}">
</td>
<td width="480px">
<label for="products{{$id->product_id}}">{{$id->products}}</label>
</td>
#endforeach
</tr>
#endforeach
Please note that if $idAndProducts is a collection then array_chunk will not work. You will need to use the inbuilt method chunk instead. Just update the first line to read
#foreach ( $idAndProducts->chunk(2) as $row )

How to loop through an array inside of array in php?

I'm trying to loop through an array inside of an array, so that I can display either phone numbers or extension number. For example: If a user has both (phone number and extension number) then I should ONLY display phone number, but sometimes a user has only a extension number then I should display the extension number.
And here's my code:
<table style="padding: 40px;margin-left: -10px;margin-top:-38px;display: inline-block;">
<div style="margin-top:16px;margin-left:10px;">
<input type="checkbox" id="checkAll"/>
</div>
<div style="padding:20px;">
#foreach($resultArray as $key => $value)
#foreach($value as $key2 => $value2)
#if(is_array($value2))
#foreach($value2 as $key3 => $value3)
<?php
// echo var_dump($value3);
if (in_array($value3['phoneNumber'], $value3)) {
if (strlen($value3['phoneNumber']) === 11) {
$value3['phoneNumber'] = ltrim($value3['phoneNumber'], 1);
}
}
else{
$value3['phoneNumber'] = $value3['extension'];
}
?>
<tr>
<td>
<input class="input_checkbox" type="checkbox"
id="{{$key3}}customer-name-checkbox" name="{{$key3}} "
value="yes"><span style="padding-left:40px;"></span>
</td>
<td>{{$value3['firstName']}} {{$value3['lastName']}}</td>
<td>{{$value3['phoneNumber']}}}</td>
<td><input style="margin-left:60px;float: right;" type="email" class="styled-text rounded" name="{{$key3}}" id="{{$key3}}customer-name-inputField" placeholder="" value=""/><br/><br/>
</td>
</tr>
#endforeach
#endif
#endforeach
#endforeach
</div>
</table>
Can someone tell me what I'm doing wrong please? Thank you so much in advance!!
From your requirements, I think you need something like this:
<?php
foreach ($resultArray['searchUserResults']['searchUserResult'] as $key => $data)
{
if (isset($data['phoneNumber']))
echo $data['phoneNumber'];
else if (isset($data['extension']))
echo $data['extension'];
}
?>
This will output either of the two, but only phoneNumber if both are present.
You do not need all the nested foreach loops to accomplish this. Instead you iterate over the sub-array only.

Categories