Laravel is there any better way to generate drop down - laravel

I am fetching data in dropdown of teachers nation from nation table using foreach loop and it's working fine but I don't think so if it's a best way, is there any more efficient way to do it?
Here is how I am doing it,
I have column(nation_id) in teacher table which connects nation table.
view:
<select name="pass_place" class="custom-select" >
<option selected value="">Choose...</option>
#foreach($nations as $nations)
<option value="{{$nations->id}}" {{$teacher->nation_id== $nations->id ? 'selected' : ''}} >{{$nations->nation}}</option>
#endforeach
</select>
controller:
public function create()
{
$teachers = Teacher::all();
$nations = nation::all();
return view('teachers.create',compact('teachers','nations'));
}
problem:
If I have 100 more tables like area, gender or nation, I will have to write code like $nations = nation::all(); in controller 100 times which is not a best way.
How can I generate dropdown without repeating code.

Related

Spring Boot Get Value of object from select using thymeleaf

So I have this project, and I want to return an object, from the previous choice of 2 select items. How can I do it? for example:
<select class="form-control" id="cityPicker1">
<option value="0">select option</option>
<option th:each="city: ${listCities}" th:value="${city.number}" th:text="${city.name}"></option>
</select>
<select class="form-control" id="cityPicker2">
<option value="0">select option</option>
<option th:each="city: ${listCities}" th:value="${city.number}" th:text="${city.name}"></option>
</select>
I want to display an item from another item list which has a number1 field and a number2 something like ticket.number1 and ticket.number2 but they have to match the values from the previous selects. I can't seem to find anything. Any ideas?
This is how I set it up in the controller:
#GetMapping("/tickets")
public String viewTicketsPage(Model model) {
List<City> listCities = cityRepo.findAll();
List<Ticket> listTickets = ticketRepo.findAll();
model.addAttribute("listCities", listCities);
model.addAttribute("listTickets", listTickets);
return "tickets";
}
I need to clarify a few things about your question.
1 - did you manage to get the checkboxes to work?
2 - can you send the selected data to the controller?
you need a controller to receive data from the page, send this data to the repository to fetch other data..
I believe you want to select a table, where the data matches the selected ones. .
take a look at JPQL, maybe this will help you:
https://www.baeldung.com/spring-data-jpa-query

Laravel Livewire Autofill Input Box Based On Drop Down Selection

I am asking for help. Is there any way I could fill an input box based on drop down selection? On my modal I have a dropdown option for subject description and I would like the subject number input field dynamically change its value. Any help would be much appreciated.
This is how I retrieved the data for my dropdown
subjects=DB::table('programs_subj')->select('corsdes', 'corsno')->get()
This is my dropdown code for the subject description which is working and the selected is saved but I could quite how to incorporate the subject number
<select name="corsdes" id="corsdes" wire:model="corsdes">
<option value="corsdes" wire:model="corsdes"></option>
#foreach($subjects as $sbj)
<option value="{{ $sbj->corsdes }}">{{ $sbj->corsdes}}</option>
#endforeach
</select>
<input name="corsno" id="corsno" wire:model="corsno"><input>
if you have the public properties $corsdes and $corsno then:
public function updatedCorsdes($value)
{
$this->corsno = $this->subjects->where('corsdes',$value)->first()->corsno;
}

How get old value of select in laravel 8 blade?

I am a beginner in Web and Laravel.I posted the same question yesterday but I delete it, because it is not clear for this I post again with more clarification. I have Select tag and a table, the table values change according to what I selected, When I select item the table is changed each time based on what I selected when I submit the Select all this works fine but the problem here is the Select lost the value I selected and goes back to the first element whose value '-1', please any suggestion to update my code to get the old value of Select after submit.
I load the same view twice with the same function the first with get an the second with post :
Route::get('/plaintes/trib', [PlainModelController::class,'suivi']);
Route::post('/plaintes/trib', [PlainModelController::class,'suivi']);
In my controller PlainModelController.php
public function suivi(Request $request)
{
$id_user = Auth::id();
$id_cour = explode('/', $id_user)[0];
$id_trib = ($request->choose_trib == "-1") ? $id_cour : $request->choose_trib;
$tribs = DB::select('SELECT CODE_TRIB as code_trib,
DESIG as desig
FROM TRIB
WHERE CODE_COUR = ? ORDER BY CODE_TRIB',[$id_cour]);
//query111 to fill the table it's work fine.
return view('suivi',compact('plaintes','tribs','id_trib'));
}
View suivi.blade.php:
<form action="/plaintes/trib" method="POST">
#csrf
<label for="choose_trib"> choose trib : </label>
<select name="choose_trib" id="choose_trib" onchange="this.form.submit()">
<option value="-1" > choose trib</option>
#foreach ($tribs as $trib)
<option value="{{ $trib->desig }}" {{ old('choose_trib') == $trib->desig ? 'selected' : '' }}> {{$trib->desig}}</option>
#endforeach
</select>
</form>
.....
I fill my table here with query111// It's work fine here.
.....

Save name instead of id from select component in laravel collective

I want to save the name of a department in the table employees, in the select component it displays the name but saves the id, so I think the problem is in it.
This is the select component:
{{Form::select('nombre_dep', $departamentos, null, array('class'=>'form-control', 'placeholder'=>'Asignar departamento'))}}
In my controller I have this for returning to the view:
$departamentos = departamento::pluck('nombre_dep', 'id');
In the model for employee I have this for relation:
public function departamentos(){
return $this->belongsTo('App\departamentos');
}
I expect to save in that field for example: production instead of 1 that could be id of the department
The selected answer is correct, but offers no explanation.
The issue is that you create your LaravelCollective Form::select so the integer id is sent in the ensuing request. You can verify this by viewing the source of the Form element in your rendered blade html. It currently will look like:
<select class="form-control" name="nombre_dep">
<option selected="selected" value="">Asignar departamento</option>
<option value="1">Production</option>
<option value="2">Department 2</option>
<option value="3">Department 3</option>
</select>
so when the action is triggered with (say) option 1 selected, your generated request simply has "nombre_dep" => "1".
You need to structure your form with the desired option value. like
<select class="form-control" name="nombre_dep">
<option selected="selected" value="">Asignar departamento</option>
<option value="Production">Production</option>
<option value="Department 2">Department 2</option>
<option value="Department 3">Department 3</option>
</select>
To do this, pass the $departamentos variable from your Controller like so:
$departamentos = array('Production' => 'Production', 'Department 2' => 'Department 2', 'Department 3' => 'Department 3');
and an easy way to do that (as the answer https://stackoverflow.com/a/56673741/8093282 stated):
$departamentos = departamento::pluck('nombre_dep', 'nombre_dep');
Perform the pluck specifying in the key parameter (the second parameter), the field from which you want to get the value to use in the value attribute of the select.
$departamentos = departamento::pluck('nombre_dep', 'nombre_dep');
Then when you pass the $departamentos array to the Laravel Collective select(), it will create an <option> element for each array element, using the array key as value attribute and the array value as option tag content.
{{ Form::select('nombre_dep', $departamentos) }}

VueJS multiple selection

I tried to multiple select with select2
i used laravel and my data on userRoles is returned data on User model with relation roles, and allRoles is just all in Role model
code on view
<select class="form-control" name="roles[]" id="roles" multiple>
<option v-for="role in allRoles"
:value="role.id"
>
#{{ role.name }}
</option>
</select>
how to set selected options using userRoles array?
Add a computed hashMap (object) to search for selected roles:
computed: {
selectedRoleMap() {
var map = {};
this.userRoles.forEach(role => {
map[role] = 1;
});
return map;
}
}
Then add :selected="selectedRoleMap.hasOwnProperty(role.id)" to options.
Working fiddle here: https://jsfiddle.net/df7j8xhz/
Alternatively, you can add a hasRole method to loop through userRoles to see if an id is in userRoles, but build a hashMap and search in it has a better performance than looping the array allRoles.length times.

Resources