Form::checkbox on Laravel 5 - php

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

Related

Laravel - Update multiple rows value with a form

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();}

PHP Laravel Two Forms sharing a Variable

Currently, after images are loaded onto the page, if the user deletes an image the page is refreshed and if they end up deleting all images then an error will appear on the screen saying that the $count variable is undefined. How can this be if the page is refreshed? I know that the form at the bottom needs to have the count variable but currently, that variable is coming from inside the first form so I can't put the form in the other.
What should I do?
{!! Form::open() !!}
... Rest of form inputs
#if (count($project->getImages()) > 0)
#foreach ( $project->getImages() as $count => $asset)
<div class="delete-toggle-container">
<i class="fa fa-times-circle-o"></i>
</div>
#endforeach
#endif
{!! Form::close() !!}
{!! Form::open(['url' => '/route/resource/' . $project->id . '/images', 'method' => 'DELETE', 'id' => 'image-delete-' . $count]) !!}
{!! Form::hidden('image', $asset, ['readonly' => true] ) !!}
{!! Form::close() !!}
Maybe you could define $count if there are no images:
#if (count($project->getImages()) > 0)
#foreach ( $project->getImages() as $count => $asset)
// Some content here
#endforeach
#else
<?php $count = 0; ?>
#endif
It's not the prettiest but it might work.
You probably should not be relying on a variable that is set via a foreach loop to exist outside of the loop. If the loop doesn't run, there is nothing to iterator over, then that variable won't be set.
Also that is a loop, do you always want the last 'key' from what was iterated as the value you need to open that form? Is that the intent or are you trying to just get an actual count to use?
Side Note, to replace the if then foreach on the same value ...:
#forelse (...)
iterating
#empty
nothing to iterate
#endforelse
If you are inside a loop you have the $loop variable available to get current index, iteration, etc ...
Laravel - Docs - Blade - Loops
Laravel - Docs - Blade - The Loop Variable

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

how to set the selected option using illuminate Form

I am using the HtmlServiceProvider in laravel 5.1 to build my form , I want to set the selected option in select dropdown list tag when I get the data from my model class
My code so far:
{!! Form::model($advertisment,['url'=>'dashboard/edit/advertisment']) !!}
{!! Form::select('Gender', [""=>'Select Gender',"Male"=>'Male', "Female"=>'Female'],0, ['class' => 'form-control', 'id' => 'form_control_6' ,'required']) !!}
{!! Form::close() !!}
Set the 0 parameter to "Male" for example and you will figure it out
If you don't want to override the value from the model, pass in null

laravel populate select box

I have seen similar questions asked before, but was unable to find a solution to my problem. So I have a ClientController and within it this function
public function edit(Client $client)
{
return view('clients.edit', compact('client'));
}
So that passes the client object to my edit view. This view is pretty much the following
{!! Form::model($client, ['method' => 'PATCH', 'route' => ['clients.update', $client->slug]]) !!}
#include('clients/partials/_form', ['submit_text' => 'Edit Client'])
{!! Form::close() !!}
So it is using a partial for the form. At the moment, the partial looks like so
<div class="form-group">
{!! Form::label('clientName', 'Client Name:') !!}
{!! Form::text('clientName') !!}
</div>
<div class="form-group">
{!! Form::label('clientStatus', 'Client Status:') !!}
{!! Form::select('clientStatus') !!}
</div>
When I visit the edit page for a client, I can see the form. The clientName is populated with the clientName value. The clientStatus is populated if I put it as a text input, but I cant get it to populate within a select as shown above. Furthermore, clientStatus can either be New or Existing. I need the select box to be pre-populated with the status of the client that is being edited, but I need the other option available within the select as well. So if the clientStatus is New, New should be pre-selected within the select box and if I open the select, Existing should be the other option.
What would be the best way to achieve this?
Thanks
Modify your select to include an array of the possible values.
Basic Select - Label is value
{!! Form::select('clientStatus',['New','Existing']) !!}
Key Value Select - Key is value
{!! Form::select('clientStatus',[ 1 => 'New', 2 => 'Existing']) !!}
Form model will then set the value in the select to the one in the model.
More information see the docs.

Categories