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
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'm newbie at Laravel and on stackoverflow. I'm making an app for registration. I want to show all those ids of section where class_id is selected in upper dropdown.
$cid=DB::table('classses')->get();
$counterr=0;
$sec=DB::table('sections')->where('class_id',$cid)->get();
$counterrr=0;
Dropdown menu for Class
<div class="col-md-6">
<select class="" name="class_id">
<option value="null">Class</option>
#foreach($cid as $cc)
#if($counterr==0)
<option selected="selected" value="{{$cc->id}}">{{$cc->title}}</option>
{$counterr++}}
#else
<option value="{{$cc->id}}">{{$cc->title}}</option>
#endif
#endforeach
</select>
Dropdown menu for Section where I want to get all the values of section where class_id is upper selected
<select section="" name="section_id">
<option value="null">Section</option>
#foreach($sec as $sc)
#if($counterrr==0)
<option selected="selected" value="{{$sc->id}}">{{$sc->title}}</option>
{{$counterrr++}}
#else
<option value="{{$sc->id}}">{{$sc->title}}</option>
#endif
#endforeach
</select>
Please help me in these case. You can freely ask anything if you want to.
You do not need to set a $counter in order to set the first select option to selected. Instead, you can check if the $key == 0:
<select class="" name="class_id">
<option value="null">Class</option>
#foreach($cid as $key => $cc)
<option {{ $key == 0 ? 'selected="selected"' : '' }} value="{{$cc->id}}">{{$cc->title}}</option>
#endforeach
</select>
Here we use a ternary expression to echo out the selected option based on if $key equals 0. If not, it will not echo out anything.
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.
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).
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.