I am working in laravel and I am passing a model into a view and I am showing one of the attributes of the model in every option but I need that the value is another one, something like showing the name of a model but I need to save the id but it it getting all the model. the code is:
<div class="form-group">
<div class="col-sm-12">
<label for="select2">Persona</label>
<select name="persona" id="persona" class="select2" required>
#foreach ($personas as $persona)
<option value="{{$persona->persona_id}}">{{$persona-
>getNombreApellidoPersona()}}</option>
#endforeach
</select>
</div>
</div>
the method getNombrePersona() is working because is returning the name and the lastname from the table, but when I return what the request give me, this what it is return in the row persona:
"tipo_usuario" => "{"tipo_usuario_id":2,"cliente":1,"nombre":"admin finca","fecha_insert":"2017-04-20 16:58:24","fecha_update":"2017-04-20 16:58:24","eliminado":0}"
And in the chrome when I check the source code in that part this is what it is shown:
<option value="{"persona_id":1,"nombre":"juan","apellido":"aguirre","correo_electronico":null,"fecha_insert":"2017-04-17 20:42:16","fecha_update":"2017-04-17 20:42:16","eliminado":0}">name last name</option>
It is like if the value that I give is replace by the whole information if the model.
I do not know why this is happening, I know that I can simply take the "persona_id" from the whole model but I want to know if it is possible to take directly from the select.
Change loop part to this:
<option value="{{ $persona->persona_id }}">{!! $persona->getNombreApellidoPersona() !!}</option>
By default, Blade {{ }} statements are automatically sent through PHP's htmlspecialchars function to prevent XSS attacks. If you do not want your data to be escaped, you may use the following syntax:
Hello, {!! $name !!}
https://laravel.com/docs/5.4/blade#displaying-data
Related
Context
I have a form built fully using a livewire component because I need to bind several inputs to do real-time calculations. I expect the dropdown items not to change, but the text input fields need to be dynamic.
Problem
when I enter a value into a binded <input> field, the previously selected items in the <select> dropdown gets reset.
Gif of the issue:
(![gif on the issue](https://i.imgur.com/FbbuiN7.gif))
I tried using the "old('VALUE')" function, but it appears to have no effect.
This is the code of "project" selector input (The stage selector code is identical):
<select id="range_project_id" name="project_id" value="{{ old('project_id') }}"
class="px-2 form-select" disabled form="create-land-registry-form">
<option selected>Choose a project..</option>
<option disabled>{ID}:{Name}</option>
#foreach (App\Models\Project::all() as $project)
<option value="{{ $project->id }}">
{{ $project->id . ': ' . $project->name }}
</option>
#endforeach
</select>
This is the code of one of the range selectors:
<div class="row">
<input wire:model.lazy="landRangeStart" type="text" name="land_id_start"
id="land_range_start" disabled form="create-land-registry-form"
class="col-3 form-control-lg border mx-2" placeholder="Starting from"
value="{{ old('land_id_start') }}" />
</div>
I tried using the "old('VALUE')" function, but it appears to have no effect.
In the parent div, add wire:ignore.self.
For example:
<div wire:ignore.self><-- Parent div -->
//Your other dropdown code in here
</div>
This directive tells Livewire to ignore changes to a subset of HTML within your component.
As you are using the raw HTML select and not a third party library, like Select2, my question is, why not bind the select value to the livewire component like so?
This would solve your problem and the component would be aware of the value from the get go.
I'm working on a simple calculator app in Laravel Blade. We have not moved to models yet, just working in views and routes, so I keep running into options I haven't learned how to use yet.
My application is running without issue but is not retaining the selected value in the dropdown on POST. I am able to print the value to the screen, and it is working in a later selector.
I think I just need to write an if statement in the options to set the selected value, but I can't find syntax that I understand/am allowed to use in this project.
<div class="form-group">
<select class="form-control form-control-lg" id="operatorInput" name="operatorInput" value="{{Session::get('operator')}}">
<option value="+" #if(Session::get('operatorInput') == "+" ? "selected" : "" )#endif>Addition (+)</option>
<option value="-" #if(Session::get('operatorInput') == "-" ? "selected" : "" )#endif>Subtraction (-)</option>
</select>
</div>
I get a throwable error on this example, so I know it isn't correct.
The throwable error is due to calling the Session Facade without the \ root indicator, use the session helper instead
For example routes/web.php
Route::get('/', function () {
session(['operatorInput' => '-']);
return view('welcome');
});
And welcome.blade.php
<div class="form-group">
<select class="form-control form-control-lg" id="operatorInput" name="operatorInput"
value="{{ session('operator') }}">
<option value="+" {{ (session('operatorInput') == "+") ? "selected" : "" }}>Addition (+)</option>
<option value="-" {{ (session('operatorInput') == "-") ? "selected" : "" }}>Subtraction (-)</option>
</select>
</div>
Result
You can keep your current code but you need to add a backslash \ before the \Session facade alias, but I find the helper function to be more elegant for a view file (and shorter)
Hope this helps
I have two tables, schools and grades and they are many to many related. I have successfully set up the many to many relationships in the model already.
I am able to insert the select box value into the database but i having difficulty try to fetch.
When i click on a grade with id = 1 for instance, i am trying to fetch its corresponding school into the select box including the values. i get an error Integrity constraint violation: 1052 Column 'id' in field list is ambiguous.
What am i not doing right in my query?
controller
public function editPage($id)
{
$grade = Grade::whereId($id)->firstorFail();
$schools = $grade->schools()->pluck('id')->toArray();
return view('grades.edit',compact('grade','schools'));
}
Model
public function schools()
{
return $this->belongsToMany('App\School')
->withTimestamps();
}
View
<div class="form-group">
<label for="select" class="col-lg-2 control-label">School</label>
<div class="col-lg-10">
<select class="form-control" id="school" name="school[]" mulitple>
#foreach($schools as $school)
<option value="{!! $school->id !!}" #if(in_array($school->id, $schools)) selected="selected" #endif >
{!! $schools->name !!}
</option>
#endforeach
</select>
</select>
</div>
</div>
$school is an array in your case, so your view most likely can't decide what to do with $school->id...
Seems more likely you wanna just use school models in your view, so just only compact('grade') and then use foreach($grade->schools as $school) in your view.
Also your conditional for selected options seems wrong, every $school (which is only an id at this point, because $schools is an array of ids) is obviously in $schools because that's how your foreach is set up. But you should fix the other thing first.
I think it's probably because it's not sure what 'id' you want to pluck.
Try
$schools = $grade->schools()->pluck('schools.id')->toArray();
You could also just pass the grade and get the schools in the view...
public function editPage($id)
{
$grade = Grade::whereId($id)->with('schools')->firstorFail();
return view('grades.edit', compact('grade'));
}
And then
$school_options = $grade->schools->pluck('name', 'id');
#foreach($school_options->all() as $key => $value)
<option value="{{ $key }}">{{ $value }}</option>
#endforeach
I am a newbie in laravel.
In my database, I have two tables, News(id, id_type) and Type(idtype,name). And in post News page, I use dropdown to select Type for my new news.
So, when I want to show the type name of this News, I do:
<div class="controls">
<select multiple name="Type">
#foreach($type as $type)
<option
#if($news->id_type == $type->idtype)
selected="selected"
#endif
value="{{$type->idtype}}">{{$type->name}}</option>
#endforeach
</select>
</div>
But I have wrong result for all type in Type tables.
Help me,please and thanks for your help!!
The single equals is for assignment while two equals is for comparison. Change this line to be
#if($news->id_type == $type->idtype)
The code block within #foreach can be modified to serve the purpose of selecting a news type corresponding to each news.
<select name="Type" id="Type" class="form-control">
#foreach($type AS $data)
<option value="{{ $data->idtype }}">{{ $data->name }}
</option>
#endforeach
</select>
The #if codeblock is not working as you think it is supposed to. So, better remove it.
Now if you want to get the type associated with each news then you can very well specify it in your Controller and get the news as per the type_id.
Code snippet from your Controller will be more helpful.
How can i get a dropdown to select whatever id is present within the database.
I have a user's table that has a location_id and a towns table that has town_id and town_name so I want the dropdown to know what the user table location_id is and show the town_name based on the id.
How can this be done in Laravel?
Thanks in advance.
What you would do is assign your user and towns to the view. Next loop your towns table to generate your select list. Something like:
<select name="location">
#foreach($towns as $town)
<option value="{{ $town->id }}" {{ ($user->location_id == $town_id) ? "selected" : "" }}>{{ $town->town_name }}</option>
#endforeach
</select>
Ok fixed it to match blade:
<select name="location" class="form-control">
# foreach($towns as $town)
<option value="{{ $town->town_id}}" #if ($user->location == $town->town_id) "selected" #else "" #endif>{{ $town->town }}</option>
#endforeach
</select>
But it's not showing the selected item
If you are sending correct data to the view and the problem is only repopulating the select then you may try this, just put this in your view and send $towns from your controller as you are sending now
{{ Form::select('towns', $towns, Input::old('towns')) }}
No need to manually loop through the array. Laravel's Form class will build the select/dropdown.
My last answer works, just find it was cached by the looks in the browser, so all fixed.