How can I get selected old values of multi-select dropdown in the create form and the edit form?
<select name="team[]" id="team" class="selectpicker" multiple>
#foreach ($tdropdown as $tdrop =>$id)
<option value="{{$id}}">{{$tdrop}}</option>
#endforeach
</select>
I tried as follows in the create form:
<option value="{{ $id }}" {{ (collect(old( 'team' ))->contains($tdrop)) ? 'selected':'' }} {{ $tdrop,$employee->team ? 'selected' : ''}} > {{ $tdrop }}</option>
In the edit form:
<option value="{{ $id }}" {{ (collect(old( 'team' ))->contains($tdrop)) ? 'selected':'' }} {{ ($tdrop,$employee->team) ? 'selected' : ''}} > {{ $tdrop }}</option>
Please help me to get the multi-select old values in the edit form and create form. Thank you.
Related
I am trying to show roles which is related to user but unfortunately userRoles are not showing related to user in selection option, please help me how can I match that thanks.
Note :- I am using spaite laravel permission docs
controller
public function edit(User $user)
{
$data = [
'isEdit' => true,
'user' => $user,
'roles' => Role::where('guard_name', '=', 'web')->select(['id', 'name'])->get(),
'userRole' => $user->getRoleNames(),
// i am getting userRole in array related to user
// ['employe']
];
// return $data['userRole'];
return view('cms.user_management.add-user', $data);
}
html view
<div class="col-md-6">
<div class="form-group">
<label>Role <span class="text-danger">*</span></label>
<select class="form-control" name="roles[]">
<option selected="selected" disabled >please
select</option>
#foreach ($roles as $item)
<option value="{{ $item->name }}"{{ $userRole ==
$item->name ? ' selected' : '' }}>{{ $item->name }}</option>
#endforeach
</select>
</div>
<span class="text-danger">{{ $errors->first('roles') ?? null }}
</span>
</div>
Try the following changes in your HTML
If $userRole is array than check if $item->name exists in array, using in_array.
'userRole' => $user->getRoleNames()->toArray(),
html view
{{ in_array($item->name, $userRole) ? 'selected' : '' }}
<div class="col-md-6">
<div class="form-group">
<label>Role<span class="text-danger">*</span></label>
<select class="form-control" name="roles[]">
<option selected="selected" disabled>please select</option>
#foreach ($roles as $item)
<option value="{{ $item->name }}"{{ in_array($item->name,$userRole) ? 'selected' : '' }}>{{ $item->name }}</option>
#endforeach
</select>
</div>
<span class="text-danger">{{ $errors->first('roles') ?? null }}</span>
</div>
you can use this to get the roles asssociated with current user or
Auth::user()->roles->pluck('name');
for any specific user roles.
User::find($id)->roles->pluck('name')
I'm trying to show the selected category in the edit form But it is not showing,
My view :
<select name="category_id" id="category_id" class="form-control{{ $errors->has('category_id') ? ' is-invalid' : '' }}">
{{--<option value="">Choose Category</option>--}}
#foreach($cat as $category)
<option value="{{$category->id}}">{{$category->name}}</option>
#endforeach
</select>
the edit method in my PostController :
public function edit($id)
{
$post = Post::findOrFail($id);
$cat = Category::all();
return view('admin.posts.edit',['post'=>$post,'cat'=>$cat]);
}
I tried to put an if statement like this :
#foreach($cat as $category)
<option value="{{ $category->id }}" {{$category->id == App\Category::class ? 'selected="selected"' : '' }}>{{ $category->name }}</option>
#endforeach
It is showing a category However it's not the selected category for the post.
HELP
$category->id == App\Category::class
You're comparing the id of the category with the name of the class here.
What you want to do is probably this:
$category->id == $post->category_id
check request by category id and if it is same echo selected.
#foreach($cat as $category)
<option value="{{$category->id}}" #if(request()->category_id==$category->id) selected #endif>{{$category->name}}</option>
#endforeach
Just retrieve it as a the defined select:
<select name="category_id" id="category_id" class="form-control{{ $errors->has('category_id') ? ' is-invalid' : '' }}">
<option value="{{ post->category->id }}" name="{{ $post->category->name }}">
#foreach($cat as $category)
<option value="{{$category->id}}">{{$category->name}}</option>
#endforeach
</select>
This will now be the default option and the loop will still be in the dropdown
I have this site with a form in it. Here I ask the user for their language skills.
First I built it like a list:
<div class="col-md-3">
<ul class="list-unstyled" id="request_profile_languages">
#isset($requestProfile)
#foreach($requestProfile->languages as $index=>$requestProfileLanguage)
<li>
<select {{ (FALSE == $canEdit) ? 'readonly':'' }} class="form-control form-select" id="{{ 'request_profile_language_'.$index }}" name="{{ 'request_profile_language_'.$index }}" placeholder="Language">
#foreach($languages as $r=>$language)
<option {{ ($language->name == $requestProfileLanguage->language->name) ? 'selected':'' }} value="{{ $language->value }}" >{{ $language->name }}</option>
#endforeach
</select>
</li>
#endforeach
#else
<li>
<select {{ (FALSE == $canEdit) ? 'readonly':'' }} class="form-control form-select" id="request_profile_language_0" name="request_profile_language_0" placeholder="Language">
#foreach($languages as $r=>$language)
<option {{ ($language->name == 'English') ? 'selected':'' }} value="{{ $language->value }}" >{{ $language->name }}</option>
#endforeach
</select>
</li>
#endisset
</ul>
</div>
There are more divs with language level and language remove.
The problem is on site, when I minimize the browser, it looks like this:
Language List
I want a table replacing this list. What am I supposed to do?
Well, you have your list in a .col-md-3 div. If you change it to a .col-xs-something and put the x's in a .column-xs beside it, then they should still match up. I'm assuming that's how it is on larger devices since you didn't post the html for the x's?
I'm currently working on an edit page. My edit perfectly shows the old (product name) input value, like so:
<div class="form-group">
<label for="product-input">#lang('product.name')</label>
<input id="product-input" type="text" name="name" class="form-control" value="{{ isset($product) ? $product->name : '' }}">
</div>
My question is, how can I do this with a select? How can I get the old selected option value when editing?
My code:
<div class="form-group">
<label>#lang('product.group')</label>
<select name="product_group_id" id="product_group_id" class="form-control" ng-model="productGroup" ng-change="changedType(val)">
<option style="display: none" value="">#lang('product.choose_group')</option>
#foreach ($product_groups as $product_group)
<option value="{{$product_group->id}}">{{$product_group->name}}</option>
#endforeach
</select>
</div>
Solution:
Added ng-selected to the option element, like so:
#foreach ($product_groups as $product_group)
<option value="{{$product_group->id}}" ng-selected="{{ isset($product->product_group_id) ? $product->product_group_id == $product_group->id : ''}}">{{$product_group->name}}</option>
#endforeach
You need to check if the old value equals option value and then use selected attribute:
#foreach ($product_groups as $product_group)
<option #if(old('product_group_id') == $product_group->id) selected #endif value="{{$product_group->id}}">{{$product_group->name}}</option>
#endforeach
Even better considering the first time with default value:
<option
value="{{$product_group->id}}"
{{ old('product_group_id', $product->id) != $product_group->id?: 'selected' }}>
{{$product_group->name}}
</option>
Trying to improve the code of my app and just migrated to L5. I used to call models in my views and I know this is not best practice - I should separate data calling and views completely.
However how do you deal with a situation like this:
<div class="field">
<label for="country">Country<sup>*</sup></label>
<select class="" id="country" name="country">
<option value="{{{ old('country') ? old('country') : '' }}}">{{{ old('country') ? App\Country::find(old('country'))->country_name : '' }}}</option>
#foreach ($countries as $studio_country)
<option value="{{ e($studio_country->id) }}">{{ e($studio_country->country_name) }}</option>
#endforeach
</select>
#if ($errors->has('country'))
<div class="alert alert-warning" role="alert">
{{ $errors->first('country') }}
</div>
#endif
<br>
</div>
Basically if there is an input and the input did not pass the validation the page refreshes with the old input and the error message. I need to extract the name of the country from the DB since I only have its ID.
You would want to do something like this:
<select id="country" name="country">
#foreach ($countries as $studio_country)
<option
value="{{ $studio_country->id }}"
{{ $studio_country->id === old('country') ? 'selected' : '' }}>
{{ $studio_country->country_name }}
</option>
#endforeach
</select>