Select option with selected values returning duplicates - laravel

I'm trying to get a old selected value and show it inside a select option, as well as a non selected value, however, when i try to compare then it doesnt seen to work, so far i can only get duplicates. What's the best way to implement it?
That's how my code look like.
Controller:
public function index()
{
$roles = Role::get();
$permissions = Permission::get();
return view('role.index', compact(['roles','permissions']));
}
Role View:
#foreach($role->permission as $permissioninrole)
<option name="permissions[]" {{ old('name', $permissioninrole->name) == $permissioninrole->name ? 'selected' : '' }} value="{{$permissioninrole->id}}">{{$permissioninrole->name}}</option>
#endforeach
#isset($permissioninrole)
#foreach($permissions as $permission)
#if($permissioninrole->name != $permission->name)
<option class="rem" name="permissions[]" value="{{$permission->id}}">{{$permission->name}}</option>
#endif
#endforeach
#endisset
#empty($permissioninrole)
#foreach($permissions as $permission)
<option name="permissions[]" value="{{$permission->id}}">{{$permission->name}}</option>
#endforeach
#endempty
How it looks. HTML
<option name="permissions[]" selected value="15">role-create</option>
<option name="permissions[]" selected value="16">role-read</option>
<option name="permissions[]" value="15">role-create</option>
<option name="permissions[]" value="16">role-read</option>
...
How it should be if role 'x' has permission role-create.
<option name="permissions[]" selected value="15">role-create</option>
<option name="permissions[]" value="16">role-read</option>
...

Solution
#foreach($permissions as $key => $value)
<option name="permissions[]" {{ in_array($key +1, $role->permission()->pluck('id')->toArray()) ? 'selected' : '' }} value="{{$value->id}}">{{ $value->name }}</option>
#endforeach

Related

How to display a select only when other select's option was chosen?-

I have 2 selects on a page and i want to show the 2nd only when in the first select a specific option was chosen, is that possible in laravel? here the code:
<select class="form-select my-2" name="category_id">
<option disabled selected>Seleziona una categoria</option>
#foreach ($categories as $category)
<option value="{{ $category->id }}" #if ($category->id == old('category_id')) selected #endif>
{{ $category->name }}
</option>
#endforeach
</select>
<select class="form-select my-2" name="type_id">
<option disabled selected>Seleziona un tipo di sushi</option>
#foreach ($types as $type)
<option value="{{ $type->id }}" #if ($type->id == old('type_id')) selected #endif>
{{ $type->name }}
</option>
#endforeach
</select>
Using JQuery :
<script>
$('select[name="category_id"]').change(function (){
let categoryId = $(this).val();
if (categoryId == null)
{
$('select[name="type_id"]').hide();
}else{
$('select[name="type_id"]').hide();
}
});
</script>

Old value in multiple select option in laravel

Here is my select option
<select name="category[]" id="categories"
class="js-example-basic-single form-control"multiple>
#foreach ($category as $element)
<option value=" {{$element->id}}}"{{ (old("category[]") == $element->id ? "selected":"") }}>
{{$element->name}}
</option>
#endforeach
</select>
I can't get the old value ,What I'm doing wrong?
how can i get the old value ?
thanks.
The variable is not named category[] on the server side, that is just notation to turn it into an array; it is named category.
As Kamlesh Paul has stated you can use in_array to check against the possible values of the old input array:
{{ in_array($element->id, (array) old('category', [])) ? "selected" : "" }}
You could try the following code:
<select name="category[]" id="categories" class="js-example-basic-single form-control"multiple>
#foreach ($category as $element)
<option value=" {{$element->id}} " {{ (collect(old('category'))->contains($element->id)) ? 'selected':'' }}>{{ $element->name }}</option>
#endforeach
</select>
you can try in_array()
https://www.w3schools.com/php/func_array_in_array.asp
<select name="category[]" id="categories" class="js-example-basic-single form-control" multiple>
#foreach ($category as $element)
<option value=" {{$element->id}}}" {{ in_array($element->id ,old("category",[]) ? "selected":"") }}>
{{$element->name}}
</option>
#endforeach
</select>

How can i Get value selected from database to edit

Let's say that I select a field the record is added to my database and my form, but when I want to change this field that I selected by another the first field is always by default, it does not return the selected field
Here was my code of create
<select name="father_id" class="form-control" required>
<option value="">--selectionner le parent svp --</option>
#foreach($fathers as $father)
<option value="{{ $eleve->father_id }}" >{{ $father->nom }}
{{ $father->prenom }}</option>
#endforeach
</select>
this is my edit form i tried this code
<select name="father_id">
#foreach($fathers as $father)
<option value="{{ $father->id }}"#if(old($father->father_id) ==
"father_id") selected #endif >{{ $father->nom }} {{ $father->prenom }}
</option>
#endforeach
</select>
my controller
public function update(Request $request, $id)
{
$note = Note::findOrFail($request->note_id);
$note->update($request->all());
session()->flash('success','Cet éléve a été modifié avec succés');
return redirect()->back();
}
the route
Route::resource('eleves','EleveController');
please help I am really stuck in this error
try it.
<select name="father_id">
#foreach($fathers as $father)
<option value="{{ $father->id }}" {{ old('father_id', '') === $father->id ? 'selected' : '' }}>
{{ $father->nom }} {{ $father->prenom }}
</option>
#endforeach
</select>

Add attribute to first item of array in Laravel

I have a select box and want to get the options of an array.
<select class="form-control select-search" name="user_select_crypto"
id="user_select_crypto">
#foreach($currencies as $currency)
<option value="{{ $currency->id }}">{{ $currency->crypto->name }}</option>
#endforeach
</select>
Now, I want to first item add the selected attribute, how can I do this?
You can use one of 2 things: Either $index => $val approach as classic foreach.
#foreach($currencies as $i => $currency)
<option value="{{ $currency->id }}" {{ $i == 0 ? 'selected' : ''}}>{{$currency->crypto->name}}</option>
#endforeach
Also as of more Laravel way, there is $loop variable you can access inside blade in #foreach
#foreach($currencies as $i => $currency)
<option value="{{ $currency->id }}" {{ $loop->first ? 'selected' : ''}}>{{$currency->crypto->name}}</option>
#endforeach
Of course, you can translate {{ $loop->first ? 'selected' : ''}} into:
#if ($loop->first) 'selected' #endif
A better way to deal with such issues is to add a default <option></option>;
<select class="form-control select-search" name="user_select_crypto" id="user_select_crypto">
<option value="" selected disabled >Select Currency</option>
#foreach($currencies as $key => $currency)
<option value="{{ $currency->id }}">{{$currency->crypto->name}}</option>
#endforeach
</select>

How to create dropdown in laravel

i want dropdown of one column in Laravel, for creating it should display all nations in drop-down, for update purpose current nation should be selected.
any example or tutorial would be helpful thanks.
My Try:
<select name="nation" class="custom-select" >
<option selected value="">Choose...</option>
<option value="{{#$teacher->nation}}" {{#$teacher->nation== "Pakistan" ? 'selected' : ''}} >{{#$teacher->nation}}</option>
</select>
Problem: my dropdown is empty.
<select name="nation" class="custom-select">
<option selected value="">Choose...</option>
#foreach($teachers as $teacher)
<option value="{{ $teacher->nation }}" {{ $teacher->nation == "Pakistan" ? 'selected' : '' }} >{{ $teacher->nation }}</option>
#endforeach
</select>
Here is the dropdown structure in laravel
<select class="form-control m-bot15" name="role_id">
#if($roles->count() > 0)
#foreach($roles as $role)
<option value="{{$role->id}}">{{$role->name}}</option>
#endForeach
#else
No Record Found
#endif
</select>
<select name="nation" class="custom-select" >
<option value="">Choose...</option>
#foreach ($teachers as $key => $value)
<option value="{{ $key }}" {{ old('nation') == $key ? 'selected' : ''}}>{{ $value }}</option>
#endforeach
</select>

Resources