Laravel Inserting Data Only the Last List or Data - php

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.

Related

For each problems Laravel Blade

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)

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?

Update status in database using checkbox - Laravel

The project can have different statuses. With the checkbox, I am trying to switch the status of the project from requested (1) to accepted (2). If the checkbox is unchecked the status is 1, checked it's 2.
When I check the checkbox I got a 419 but this is normally related to the token but I added a #csfr field. Why is the status not changed in the database?
Thanks for any help.
index.blade.php (summary of all the projects)
#foreach ($projects as $project)
<tbody>
<tr>
<form action="/projects/plan" method="post" id="statusForm">
#csrf
<input name="id" type="hidden" value="{{$project->id}}">
<td>
<input type="hidden" value="{{$project->status}}" name="status">
<input {{isset($project['status']) && $project['status'] == '2' ? 'checked' : ''}}
value="{{$project->status}}" type="checkbox" name="status"
onchange="document.getElementById('statusForm').submit()"
>
</td>
</form>
<td>{{$project->applicant_name}}</td>
<td>{{$project->project_name}}</td>
<td>Project Details</td>
</tr>
</tbody>
#endforeach
Project.php (functions to update status)
const STATUS_requested = 1;
const STATUS_ACCEPTED = 2;
public function updateStatus( $status )
{
$this->update([
'status' => $status
]);
$this->refresh();
return $this;
}
public function projectAccept() {
return $this->updateStatus( self::STATUS_ACCEPTED );
}
ProjectsController.php (dd('hello') is not printed it seems like data is not sent to this resource)
public function plan(Request $request)
{
dd('hello');
Event::find($request->id)->projectAccept();
return Project::STATUS_ACCEPTED;
}
web.php
// Update status project
Route::post('/projects/plan', 'ProjectsController#plan');
First, you cant select by ID document.getElementById('statusForm').submit() when you have multiple DOMS with the same ID.
change your loop to something like this
#foreach ($projects as $project)
<tbody>
<tr>
<td>
<form action="/projects/plan" method="post" id="statusForm{{$project->id}}">
#csrf
<input name="id" type="hidden" value="{{$project->id}}">
<input {{isset($project['status']) && $project['status'] == '2' ? 'checked' : ''}}
value="2" type="checkbox" name="status"
onchange="document.getElementById('statusForm{{$project->id}}').submit()"
>
</form>
</td>
<td>{{$project->applicant_name}}</td>
<td>{{$project->project_name}}</td>
<td>Project Details</td>
</tr>
</tbody>
#endforeach
Now, a checkbox will only be sent in a form when it is checked, so no need for a variable value for that input
<input {{isset($project['status']) && $project['status'] == '2' ? 'checked' : ''}}
value="2" type="checkbox" name="status"
onchange="document.getElementById('statusForm{{$project->id}}').submit()"
>
Finally, when you recover the input status, set a default value for the unchecked one (you can remove the hidden input with this one). Or as you did, set a hidden input with the original value to be sent every time. Both solution are perfect.
public function plan(Request $request)
{
$status = $request->input('status', Project::STATUS_requested);
Event::find($request->id)->projectAccept();
return Project::STATUS_ACCEPTED;
}
That way if it is checked it will be 2 (in the request) and if not, it will be 1 from the default value.

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...

how to make radio buttons behave in a mutually exclusive way

I was to trying to update my checkbox value based on checked..But the problem I'm facing is i can't uncheck the old checkbox checked ..So when i click on the new checkbox the old one should be uncheck...I have used laravel platform where table header comes from database and from database the old checkbox value came..
here is my blade view :
<table class="table">
<thead>
<tr>
<th>SL No</th>
<th>Name</th>
#foreach($roles as $row)
<th>{{$row->label}}</th>
#endforeach
</tr>
</thead>
<tbody>
<?php $i=1; ?>
#foreach($users as $user)
#foreach($user->roles as $row)
<tr>
<td>{{$i}}</td>
<td>{{$user->name}}</td>
#foreach($roles as $role)
<td>
#if($role->role == $row->role)
<input type="checkbox" class="myCheckbox" checked="" >
#else
<input type="checkbox" class="myCheckbox">
#endif
</td>
#endforeach
</tr>
<?php $i++; ?>
#endforeach
#endforeach
</tbody>
</table>
Here is the javascript i used for check only one checkbox .
$('.myCheckbox').click(function() {
$(this).siblings('input:checkbox').prop('checked', false);
});
anybody could help me to find out the solution please?
Either use radio buttons, or iterate over all checkboxes setting the checked value to false and only checking the just clicked checkbox.
Here is an example.
var container = document.querySelector('.container');
container.addEventListener('click', function(evt) {
[].slice.call(this.children).forEach(function(input) {
input.checked = false;
})
evt.target.checked = true;
})
<div class="container">
<input type="checkbox"> Foo
<input type="checkbox"> Bar
<input type="checkbox"> Baz
</div>

Categories