Laravel - Update multiple rows value with a form - php

I am listing many rows in a table and I AM trying to edit them with a drop down menu that exists in each row. The code below is fine but it will store only the last (S_Rank) value (which is the column I want to change) what would be the best way to get each row input. I know the issue that the form is not an array, what would be the way to make it an array
My view
{!! Form::open(['action' => 'AbstractsController#UpdateRank' , 'method' => 'post' ]) !!}
<table id="myTable" class ="table table-striped">
<thead>
<td><h4>Student Name</h4></td>
<td><h4>Student Rank</h4></td>
</thead>
#foreach($applications as $application)
<tbody>
<tr>
<td><h5>{{$application->Student_Name}}</h5></td>
<td><h5>
{{Form::select('Ranking' ,$ranks, ['class' => 'form-control', 'placeholder' =>$application->S_Rank] )}}
{{Form::hidden('Student_ids[]', $application->S_ID)}}
</h5></td>
</tr>
#endforeach
</tbody>
</table>
{{Form::Submit('Save New Ranking',['class' => 'btn btn-primary'])}}
{!! Form::close() !!}
My controller
foreach(request('Student_ids') as $S_ID){
$application = Application::where('S_ID' , $S_ID)->get();
$application->S_Rank = $request-> input('Ranking');
$application->save();}
I am trying to update each row with the inputted value from each dropdown menu

I think you already answer yourselve. I think what you need is to change your $ranks var to $ranks[] because you are actually only taking the last value they chose in your select, that way, you are storing your ranks in a one dimenssion array that should correspont with your Student_ids[] hidden array

If you want to update the ranking per application, you'll need to know which ranking value goes to which application. Assuming S_ID is a unique identifier for Application, you could do something like this in your view:
<h5>
{{Form::select('Ranking[' . $application->S_ID . ']' ,$ranks, ['class' => 'form-control', 'placeholder' =>$application->S_Rank] )}}
</h5>
Then in your controller:
foreach(request('Ranking') as $S_ID => $rank){
$application = Application::where('S_ID' , $S_ID)->first();
$application->S_Rank = $rank;
$application->save();}

Related

Data Pass from controller to view

I want to display the sum of column debit. When I pass data after sum value of the debit column and store it in a variable, I pass the data from controller to my view but laravel says it's undefined. Can anyone help me with this?
View.blade.php
<tr>
<th>Total Debit:</th>
<td>
#foreach ($totaldebit as $tdebit)
{{ $tdebit }}
#endforeach
</td>
</tr>
UserController.php
$tdebit = DB::table('debitemps')->sum('debit');
return view('pages/payments/debit/debit', [
'workerlists' => $data,
'debitid' => $id,
'Empdebitdata' => $dataEmp,
'totaldebit' => $tdebit
]);

Update specific table field on another Table

I need to update specific one id / row on a separate field on another table. how is the logic to that? so far i have this pseudo code sql query.
table name: settings
current table: aircrafts
UPDATE settings SET description id = 4
i need to update id 4 on my database in database settings
how can i integrate this code into laravel syntax?
assuming i have this controller
public function editAirReg(Request $request, $id)
{
$descr = $request->input('descr_id');
DB::update('update settings set description = ,'$descr', where id = 4',[$name,$id]);
return redirect('/admin/aircrafts')->with('success', 'Settings Updated');
}
and also this view
{!! Form::open(['action' => 'Admin\AircraftsController#getdata', 'method' => 'POST']) !!}
<input type="hidden" class="form-control" name="settings" value="{{$aircraft->air_line}}">
{{Form::submit('BIND', ['class'=>'btn btn-primary btn-block btn-lg', 'name'=>'submit'])}}
{!! Form::close() !!}
First of all, getData() doesn't make sense if you're updating something in mysql. It should be called store() or save or whatever you want to call so that the function name is appropriate.
Let's change the form so that it can pass the id to the controller.
{ Form::open(array('action' => 'Admin\AircraftsController#getdata', $id)) }}
{{ Form::hidden('settings', $aircraft->air_line) }}
{{ Form::submit('Submit', array('class' => 'btn btn-primary')) }}
{{ Form::close() }}
What I did is that change your input to the laravel syntax. Also make sure you pass the id from form to controller. take a look at my example. look where I put $id field.
Now in the controller, this is what you do.
$setting = Setting::findOrFail($id); // you must have a model or you can use DB facade
$setting->description = $request->settings; //name that you called it in form
$setting->save();

Laravel - Form select multiple

what I am trying to do is to convert standard Blade select form with multiple options where you must select options with ctrl pressed to one where you use checkboxes to select multiple choices.
My form look like this:
{!! Form::open(array('action' => 'UserController#visit_step_two', 'method' => 'post')); !!}
{!! Form::select('services', $servicesList, null, array('multiple'=>'multiple','name'=>'services[]')); !!}
{!! Form::submit('Next'); !!}
{!! Form::close(); !!}
What should I do to change that?
The Form::select() receives an array or collection which it then iterates over to generate all the <select> element's options. If you want to use checkboxes instead, you need to iterate manually to create each checkbox:
{!! Form::open(array('action' => 'UserController#visit_step_two', 'method' => 'post')); !!}
#foreach ($servicesList as $value => $name)
{!! Form::checkbox('services[]', $value, ['id' => 'service' . $value]); !!}
{!! Form::label('service' . $value, $name) !!}
#endforeach
{!! Form::submit('Next'); !!}
{!! Form::close(); !!}
The above will create a form with a list of checkboxes that have labels attached. The id attribute is added so that you can also click on the label to select the associated checkbox, since the first parameter of Form::label() will be used to generate the for attribute of the <label> which is associated with a checkbox <input> field that has the same value for the id, and since an id has to be unique it is generated using the value like so 'service' . $value because all values should also be unique.
You need jquery plugin to do that, or you can write your own.
I know a plugin is Multiple Select http://wenzhixin.net.cn/p/multiple-select/docs/#checkall-uncheckall

Form::checkbox on Laravel 5

I just started learning laravel and I have the following situation:
I have a table which is called 'cities' and a table 'users'. To connect them I have a table users_cities.
I am trying to do a form using laravel
#extends('welcome')
#section('content')
<h1>Cities</h1>
#foreach ($cities as $city)
{!! Form::checkbox($city->name, $city->id, null, ['class' => 'field']) !!}{!! $city->name !!}
#endforeach
#foreach ($citiesSelect as $citySelect)
{!! $citySelect->id !!}
#endforeach
#stop
And it`s bringing the name of the cities on the first foreach, and on the second is bringing the ids of the cities that should be checked.
Basically is bringing the entries on the database which has the id_user selected.
But I can't figure out how to bring the checkboxes on the first foreach selected using the result of the second foreach.
first create array of city ids like this
$selectedCitiesIds = array_map(function($city) {
return $city->id;
}, $citiesSelect);
then
{!! Form::checkbox($city->name, $city->id, (in_array($city->id, $selectedCitiesIds)), ['class' => 'field']) !!}{!! $city->name !!}
simply add a 'true' argument to your statement like such:
Form::checkbox($city->name, $city->id, true, ['class' => 'field'])
You can do this too.
In this case you have an array called cmbbolsas and in the checkbox input you can use it
#foreach ($cmbbolsas as $key=>$value)
<input type="checkbox" name="bolsas" value={{$key}}> {{$value}}</br>
#endforeach
I hope it works for you :3

Laravel: forms and radiobuttons

I am just wondering if i could ask a fairly simple question about radio buttons and Laravel as the googlebox isnt co-operating. My code is thus:
{{ Form::open(array('url'=>'users/create', 'method' => 'post')) }}
<tbody>
<label><span> </span>{{ Form::submit('Approve/Decline')}}</label>
#foreach($users as $user)
<tr>
<td>{{{$user->firstname}}}</a></td>
<td>{{{$user->secondname}}}</a></td>
<td>{{{$user->address}}}</a></td>
<td>{{{$user->city}}}</a></td>
<td>{{{$user->phone}}}</a></td>
<td>{{{$user->id}}}</td>
<td>{{{$user->username}}}</a></td>
<td>{{{$user->email}}}</tds>
<td>{{{$user->type}}}</a></td>
<td>{{Form::radio('Membership[]', 'approve')}}</a></td>
<td>{{Form::radio('Membership[]', 'decline')}}</a></td>
</tr>
#endforeach
</tbody>
{{ Form::close() }}
A simple form with my two text boxes at the end there. This turns on fine in the browser but once i check one radio button the others deselect which would be fine in this order:
O O
but since its in a loop, there are many rows of O O and i can only choose one out of all of the different rows. I would imagine I would need to group each two together somehow dynamically but I'm unsure. All aid appreciated.
Probably you'll need to set a different name for each of your checkboxes/radio buttons:
<td>{{Form::radio("Membership[".$user->id."]", 'approve')}}</a></td>
<td>{{Form::radio("Membership[".$user->id."]", 'decline')}}</a></td>
To get your values in your controller you just have to:
dd( Input::get('Membership')[$user->id] );
In return you should get
approve
or
decline
Note that for this to work this way I removed the quotes from
"Membership[".$user->id."]"
Here are two routes I used to test it:
The form:
Route::any('test', function() {
return Form::open(array('url' => 'radio', 'method' => 'post')) .
Form::radio("Membership[1]", 'approve') .
Form::radio("Membership[1]", 'decline') .
Form::submit('send') .
Form::close();
});
The result:
Route::any('radio', function() {
dd( Input::get('Membership')[1] );
});

Categories