How can i Get value selected from database to edit - laravel

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>

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>

How can I set the default value Laravel <select> element?

I want to retrieve database values in category name and i want to show default value in selection. This is my controller for my edit view.
I have a category_product table.
<div class="col-md-4">
<div class="form-group">
<label for="category">category</label>
<select class="form-control" name="category" id="category">
#foreach($categories as $category)
<option value="{{ $category->id }}" {{ $product->categories()->category_id == $category->id ? 'selected' : '' }}>{{ $category->name }}</option>
#endforeach
</select>
</div>
</div>
ProductController.php
public function edit(Product $product)
{
$categories = Category::all();
return view('Admin.products.edit', compact('product', 'categories'));
}
Product.php
public function categories()
{
return $this->belongsToMany(Category::class);
}
I get this error.
Undefined property: Illuminate\Database\Eloquent\Relations\BelongsToMany::$category_id
You need to add default value in the select tag above foreach() loop.
<div class="col-md-4">
<div class="form-group">
<label for="category">category</label>
<select class="form-control" name="category" id="category">
<option value="xyz">xyz</option> /* set default option value */
#foreach($categories as $category)
<option value="{{ $category->id }}" {{$category->id == $category->id ? 'selected' : '' }}>{{ $category->name }}</option>
#endforeach
</select>
</div>
</div>
If you want set category name selected then you need to match category in product collection. ( if you have catetory_id in product table )
<select class="form-control" name="category" id="category">
#foreach($categories as $category)
<option value="{{ $category->id }}" {{$product->catetory_id == $category->id ? 'selected' : '' }}>{{ $category->name }}</option>
#endforeach
</select>

Select option with selected values returning duplicates

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

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>

Laravel dropbox initial value on update

Items belongs to Categories. On edit page for Items I have an html select with Categories. Is this the best solution to have the right initial state for this select?
<select name="id_category" class="form-control">
#foreach($categories as $category)
#if($category->id == $item->id_category)
<option value="{{ $category->id }}">
{{ $category->name }}
</option>
#endif
#endforeach
#foreach($categories as $category)
#if($category->id != $item->id_category)
<option value="{{ $category->id }}">
{{ $category->name }}
</option>
#endif
#endforeach
</select>
I'm not sure i understand your question, but i think you want to select item was previously select by user, so you can use inline if to set selected.
<select name="id_category" class="form-control">
#foreach($categories as $category)
<option value="{{ $category->id }}" {{ $category->id == $item->id_category ? "selected" : "" }} >
{{ $category->name }}
</option>
#endforeach
</select>
Sorry if i didn't understand your question

Resources