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>
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 am creating a website using laravel and I have a form to create a new users and within it i need an option to display a way to select a users role, i would like to do this in the form of a drop down list but I'm having difficulty figuring it out.. so far I have this (see below my create.blade.php), but it is not displaying the data from the three roles i have in the table in the DB, (these are admin,instructor and student).
<div class="form-group">
<label for="role">Role</label>
<select name="roles[]" class="form-control">
#foreach($roles as $role)
<ul class="dropdown-menu" role="menu">
{{ $role }}
</ul>
#endforeach
</select>
</div>
Below is my form, I am new to laravel so just trying to learn to better myself, any help is much appreciated :)
If you're using native HTML select, you may use
<div class="form-group">
<label for="role">Role</label>
<select name="roles[]" class="form-control">
#foreach($roles as $role)
<option value="{{ $role->id }}"> {{ $role->nameOrWhatever }} </option>
#endforeach
</select>
</div>
In your UserController
/**
* Show the form for creating a new user
*
* #param \App\Role $model
* #return \Illuminate\View\View
*/
public function create(Role $model)
{
return view('users.create', ['roles' => $model->get(['id', 'name'])]);
}
then, in your create.blade.php
<select name="role_id" id="input-role" required>
<option value="">-</option>
#foreach ($roles as $role)
<option value="{{ $role->id }}" {{ $role->id == old('role_id') ? 'selected' : '' }}>{{ $role->name }}</option>
#endforeach
</select>
This will get you the desired
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 have database with types such as:
Entertainment & Food,
Health & Care
etc.
I insert it into a dropdown like this:
<div class="form-group{{ $errors->has('type') ? ' has-error' : '' }}">
<div class="type_message form-control alert-warning" style="display: none;"></div>
<label id="type2" for="type" class="col-md-4 control-label">Type</label>
<div class="col-md-6">
<select class="form-control" name="type" id="type">
<option selected value="{{ $entity->type }}">{{ $entity->type }}</option>
#foreach ($entities as $entity_select)
<option value={{ $entity_select->type}}>{{ $entity_select->type }}</option>
#endforeach
</select>
#if ($errors->has('type'))
<span class="help-block">
<strong>{{ $errors->first('type') }}</strong>
</span>
#endif
</div>
</div>
The problem is that when I check the value of the field it looks like this:
<option value="Entertainment & Nightlife" selected="">Entertainment & Nightlife</option>
Therefore in the database it only adds "Entertainment" how can I avoid that?
<option value="{!! $entity_select->type !!}">{!! $entity_select->type !!}</option>
https://laravel.com/docs/5.4/blade#displaying-data
As mentionned in the documentation :
Be very careful when echoing content that is supplied by users of your
application. Always use the escaped, double curly brace syntax to
prevent XSS attacks when displaying user supplied data.
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>