I have stored multiselect values in database as string
Now while trying to edit the values in need to fetch this highlighted string as array and send it to edit view , so that i can populate multi select with this selected values.
My code in view for multi select
<div class="form-group">
<label class="control-label col-md-3">News For
<span class="required" aria-required="true"> * </span>
</label>
<div class="col-md-4">
{!! Form::select('news_todisplay[]',['users'=>'Users','staff'=>'Staff','cinemahall'=>'Cinemahall'],$todisplayarray,array('class'=>'form-control','multiple'=>'multiple')) !!}
</select>
</div>
</div>
Where $todisplayarray is the array fetched from database i.e they are users,staff, cinemahall
My controller code
public function edit($id)
{
$newsdetails=General_news::findOrFail($id);
$todisplayarray=explode(',',$newsdetails->news_todisplay);
return view('admin.editnews',compact('newsdetails','todisplayarray'));
}
Page view-source shows this
<select class="form-control" multiple="multiple" name="news_todisplay[]"><option value="users">Users</option><option value="staff">Staff</option><option value="cinemahall" selected="selected">Cinemahall</option></select>
This only shows last value as selected, where as it should display 3 of the values as selected.
Kindly let me know where I am going wrong.
Find the below answer
<?php
//get the old values from form
$old = old('category');
//get data from database table field
$cate= explode(',',$data->category_ids);
//stay the values after form submission
if($old){ $cate=$old; }
?>
<select id="categories" name="category[]" multiple>
#foreach ($categories as $val)
<option value="{{ $val->id }}" <?php echo in_array($val->id,$cate)?"selected" :"" ;?> >{{ ucfirst($val->category_name) }}</option>
#endforeach
</select>
I've used loop the categories data with id's and match the data stored in the table
Related
I have a select where a value needs to be selected. Select is formed using a foreach loop. I get the values correct and correct, but when I try to put this value into the session, only the last value of the cycle gets into it all the time. Why is that?
<div class="form-group">
<label for="category">Choose category</label>
<select class="form-control" id="category" name="category_slug" onchange = "getSelectValue();">
#foreach($categories as $category)
<option value="{{$category->slug}}">{{$category->name}}</option>
{{session(['key' => $category->slug])}}
#endforeach
</select>
#dump(session('key'))
</div>
In your loop you overwrite the key with each iteration so that only the last category is put into the session.
If you want to put all the categories into the session you should give each item an individual session key like this:
<select class="form-control" id="category" name="category_slug" onchange = "getSelectValue();">
#foreach($categories as $index => $category)
<option value="{{$category->slug}}">{{$category->name}}</option>
{{session(['category_'.$index => $category->slug])}}
#endforeach
</select>
Basically I am creating a filter button to return query based on the string that was saved on the database. Here is my code:
<div class="input-group mb-3">
<select class="custom-select" id="inputGroupSelect">
<option selected>Filter By Name</option>
#foreach ($events as $event )
<option value="{{ $event->event_parts }}">{{ $event->event_parts }}</option>
#endforeach
</select>
<div class="input-group-append">
<button class="btn btn-outline-secondary" type="button">Filter</button>
</div>
</div>
Since they are strings, how do I create a distinct select option field retrieving only unique strings from the database?
I tried using distinct() and unique() but both returned with errors.
change $events to $events->unique('event_parts'), which would filter out al non-unique $events based on the event_parts parameter (assuming it is a parameter, otherwise you'd use ->filter() and with a callback function).
I am working on jquery select2 dropdown. I have to display the multiple selected option in select area. For this purpose i loop through each of select2. If condition is met i will assign 'selected' attribute property to option so that it may appears in the select area.
But somehow the select2 fails to display the selected ''.
HTML CODE:
<div class="form-group col-md-12">
<label for="inputsskill">Skill<span class="required-field">*</span></label>
<fieldset>
<select class="custom-select w-100" name="skill_name[]" id="skill_name" multiple="multiple" style="width: 100%;">
#if(!$skills->isEmpty())
#foreach($skills as $skill)
<option value="{{$skill->id}}">{{$skill->name}}</option>
#endforeach
#endif
</select>
</fieldset>
</div>
JQUERY CODE:
// parse skills
var parseData = JSON.parse(skill);
// parseData is an array which contain 1,2,3 values like ['1','2','3']
$("#skill_name > option").each(function() {
// if select2 and array values matches assign selected attribute to that option
if( $.inArray($(this).val(), parseData) !== -1 ) {
$(this).attr('selected', 'selected');
}
});
Select2 Jquery:
$( "#skill_name" ).select2({
});
I dont know where i am going wrong, i would appreciate if someone guide me in this.
Thanks,
you can achieve this functionality with PHP easily.
E.g : fetch skills from the database which you want to preselect and create an array of skills. Then match this in your loop.Like
<div class="form-group col-md-12">
<label for="inputsskill">Skill<span class="required-field">*</span></label>
<fieldset>
<select class="custom-select w-100" name="skill_name[]" id="skill_name" multiple="multiple" style="width: 100%;">
#if(!$skills->isEmpty())
#foreach($skills as $skill)
<option value="{{$skill->id}}" <?php
if(in_array($skill->name, $selectedskillsArray)){
echo $selected = 'selected';
}
?> >{{$skill->name}}</option>
#endforeach
#endif
</select>
</fieldset>
</div>
Try this one.
Jquery solution
$( "#skill_name" ).select2({
});
$('#skill_name').val(["1","2","3"]).trigger('change');
I'm currently using Laravel and I want to have a dropdown box display category names from my database, but when selected the value it returns to the server should be a numerical value of the name selected which is also stored in the database.
Like so:
#foreach($categories as $category)
<option name="pid" value="$category->id" id="cname">{{$category->name}}</option>
#endforeach
The values are passed from my Controller to the Blade template. As you can see, I want it to display the category names in the drop down (which it does fine), but once selected and submitted, I want the category ID to be returned. Since the "value" attribute returns the value, I want to have the variable $category->id as the "value". I want to do this without using JavaScript or jQuery. Only with PHP. How would I go about accomplishing this and is it even possible?
When I try to use value="{{$category->id}}" I get the Laravel error:
Integrity constraint violation: 1048 Column 'parent_id' cannot be null
So it seems like it isn't submitting the value of the dropdown.
Here is the code I'm currently using in my Blade template:
<form class="" action="{{route('createsubcategorypost')}}" method="post">
<div class="form-group">
<label for="cname">Sub-Category name:</label>
<input type="text" name="cname" value="" placeholder="Sub-Category Name" id="cname" class="form-control">
</div>
<div class="form-group">
<label for="pid">Parent ID</label>
<select class="form-control">
#foreach($categories as $category)
<option name="pid" value="{{$category->id}}" id="pid">{{$category->name}}</option>
#endforeach
</select>
</div>
<div class="form-group">
<center>
<button type="submit" name="button" class="btn btn-success">Create Sub-Category</button>
</center>
</div>
{{csrf_field()}}
</form>
And this is the code in my Controller:
public function CreateSubCategoryPost(Request $request){
if ($request->cname == null) {
session()->flash('errormessage',' Invalid sub-category name.');
return redirect()->back();
}
$scc = SubCategory::where('name',$request->cname)->first();
if ($scc !== null) {
session()->flash('errormessage',' Sub-category with that name already exists.');
return redirect()->back();
}
$subCategory = new SubCategory;
$subCategory->name = $request->cname;
$subCategory->uniqueid = 'SCA'.str_random(28);
$subCategory->slug = str_slug($request->cname,'-');
$subCategory->parent_id = $request->pid;
$subCategory->save();
return redirect()->route('categories');
}
Same way you you are displaying the category name inside the option tag :
#foreach($categories as $category)
<option name="pid" value="{{$category->id}}" id="cname">{{$category->name}}</option>
#endforeach
Everything you call from a database in Laravel should have the format {{$category->someName}} no matter where you place it in your view. In your case you need to enclose $category->id in {{ }}
I have a form where I put de id's of my bands table inside a select option. When I select an id in the dropdownlist and try to submit the form, the value always remains NULL. How can I retrieve the value I selected?
create.blade.php file :
<form>
<select name="band_id">
#foreach ($bands as $band)
<option value="{{ $band->band_id }}">{{ $band->band_id }}</option>
#endforeach
</select>
<input type="submit" value="Submit">
</form>
My Bandcontroller inside my store action :
$bands->band_id = $input['band_id'];
First of all you are using #foreach ($band as $band), make sure it's not a typo and if not then fix it (Could be $bands as $band and assumed you know what I mean). Anyways, you may also try this:
{{ Form::select('band_id', $bands, Input::old('band_id')) }}
Just pass the $bands variable to the View and Laravel will create the SELECT for you, you don't need to loop manually. To retrieve the value, you may try this:
$band_id = Input::get('band_id');
Laravel 6 or above
//blade
{!!Form::open(['action'=>'CategoryController#storecat', 'method'=>'POST']) !!}
<div class="form-group">
<select name="cat" id="cat" class="form-control input-lg">
<option value="">Select App</option>
#foreach ($cats as $cat)
<option value={{$cat->id}}>{{ $cat->title }}</option>
#endforeach
</select>
</div>
<div class="from-group">
{{Form::label('name','Category name:')}}
{{Form::text('name','',['class'=>'form-control', 'placeholder'=>'Category name'])}}
</div>
<br>
{!!Form::submit('Submit', ['class'=>'btn btn-primary'])!!}
{!!Form::close()!!}
// controller
public function storecat(Request $request){
Log::channel('stack')->info('name'.$request->app);
if($request->input('cat')==null){ // retriving option value by the name attribute of select tag
return redirect(route('cats.cat'))->with('error', 'Select an app');
}
$this->validate($request,[
'name'=>'required',
]);
}