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.
Related
I have a select input that allows for multiple options to be selected. Because of the simple requirements, these selections are just stored as an array in my MySQL field. So for example they are stored as:
[Retail, Wholesale]
Now my question is, if I wanted the user to be able to edit the specific record, how would I pull the options up as selected in the select input?
The select field is as such:
<select class="form-control" name="type_industry">
<option value="Retail">Retail</option>
<option value="Wholesale">Wholesale</option>
<option value="Service">Service</option>
</select>
This is in a Laravel blade template, but if necessary I'm willing work with pure php answers or even javascript, I just need to be able to move past this issue.
Thanks!
You can use in_array() function,
<select class="form-control" name="type_industry">
<option value="Retail" #if(in_array("Retail", $selected_items_array)) selected #endif>Retail</option>
<option value="Wholesale" #if(in_array("Wholesale", $selected_items_array)) selected #endif>Wholesale</option>
<option value="Service" #if(in_array("Service", $selected_items_array)) selected #endif>Service</option>
</select>
if [Retail, Wholesale] is a php array ($savedOptions), and you have a list of all available options ($availableOptions) as another array, pass them to the blade view and in your blade just loop through:
#foreach ( $savedOptions as $value )
<option {{ in_array($value,$availableOptions) ? 'selected' : '' }} value={{ $value }}>{{ $value }}</option>
#endforeach
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
I've a form to be completed to create a post. Now after creating that post, if I want to edit it, I'm showing a select option in the edit page something like below. (I am using Laravel)
<select name="posts">
#foreach($posts as $post)
<option value="{{$post->id}}"> {{$post->name }} </option>
#endforeach
</select>
Now I need to preselect the populated select field with the current post name. I have a post id in the URL which I can get know in which post I am in. How can I select the right option without making a duplicate of the same in the options field. ?
Assuming you pass the current post id into the template as $postId, you could do something like this:
<select name="posts">
#foreach($posts as $post)
<option value="{{$post->id}}" {{ ($post->id == $postId) ? 'selected="selected"' : '' }}> {{$post->name }} </option>
#endforeach
</select>
Also, since you're using Laravel, a little bit cleaner solution is to use Laravel's Form builder:
{{ Form::select('posts', $posts, $postId) }}
Use an if-else structure inside the loop.
if ( post is equal to the current post ) {
<option selected="selected" value="{{$post->id}}"> {{$post->name }} </option>
} else {
<option value="{{$post->id}}"> {{$post->name }} </option>
}
The condition depends on you, whether you want to use the post id or the post name to check. (Whatever you have available).
Hi all I am trying to set the value of a dropdown to selected when the user goes to an edit page of my application but currently I am getting an error and I realise it is because I do not have the correct syntax.
This is my code:
<div class="form-group">
<label>Category</label>
<select name="category_id" id="cat_drop">
#foreach($cats as $cat)
<option value="{{$cat->id}} {{if($tip->category_id == $cat_id){ echo 'selected';} }}">{{$cat->name}}</option>
#endforeach
</select>
</div>
Basically Tips have a category_id and the dropdown allows a user to change the tip category. However I would like for the currently selected category_id to be set as the value of dropdown.
I am new to Laravel and the Blade Templating engine so would appreciate any help.
You can use a ternary if:
#foreach($cats as $cat)
<option value="{{$cat->id}}" {{($tip->category_id == $cat_id) ? 'selected':''}}>{{$cat->name}}</option>
#endforeach
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.