I want to edit multi select for which i want to pass two loops to
bootstrap modal and their i want to compare id's and on that basis i will select already selected record my button
<a type="button" data-toggle="modal" data-target="#editModal{{ $value->id}}">
<span class="glyphicon glyphicon-edit"></span>
</a>
my form
<div class="form-group">
<select name="upsubject[]" multiple class="form-control">
#foreach($subject as $data)
#foreach($value->subject as $key)
<option value="{{$data->id}}" <?php if ($key->id == $data->id)
{echo
"selected";}
?>>{{$data->name}}</option>
#endforeach
#endforeach
</select>
</div>`
try this:
<option {{ ($key->id == $data->id) ? 'selected' : '' }}>{{ $data->name }}</option>
I have to detach the selected and than select new records as i am updating subjects but the above logic give me same subject three times each
Related
I am trying to implement a dependent dropdown using Laravel Livewire along with Jetstream. Everything worked fine, but there is one minor problem though. When I click on edit button, I am trying to fill the given details from the database. But unfortunately, the dropdown details do not fill. However, the text input fetches the value and displays it.
Here is the blade file:
<form>
<div class="form-group">
<x-jet-label for="countryName">Select Country</x-jet-label>
<select class="form-control" name="country_name" id="countryName" wire:model="countryId">
<option value="">-- Select a country --</option>
#foreach($countries as $country)
<option value="{{ $country->country_id}}">{{ $country->country_name }}</option>
#endforeach
</select>
#error('country_id') <span class="text-danger">{{ $message }}</span>#enderror
<x-jet-label for="stateName">Select State</x-jet-label>
<select class="form-control" name="state_name" id="stateName" wire:model="stateId">
<option value="">-- Select a state --</option>
#if (!is_null($states))
#foreach ($states as $state)
<option value="{{ $state->state_id}}">{{ $state->state_name }}</option>
#endforeach
#endif
</select>
#error('state_id') <span class="text-danger">{{ $message }}</span>#enderror
<x-jet-label for="cityName" style="">City Name:</x-jet-label>
<x-jet-input type="text" name="city_name" id="cityName" placeholder="Enter City Name" wire:model="city_name" />
#error('city_name') <span class="text-danger">{{ $message }}</span>#enderror
</div><br>
<x-jet-button wire:click.prevent="update()" class="btn btn-success">Update</x-jet-button>
<x-jet-danger-button wire:click.prevent="cancel()" class="btn btn-danger">Cancel</x-jet-danger-button>
</form>
And edit function is like:
public function edit($city_id)
{
$city = City::findOrFail($city_id);
$this->city_id=$city_id;
$this->city_name=$city->city_name;
$this->countryId=$city->country_id;
$this->stateId=$city->state_id;
$this->updateMode = true;
}
When I select any option manually from the dropdown, it works.
I am new to laravel and am stuck here... Any help would be highly appreciated. Thank you.
I try to get the selected option for the Team select on my form to further use it in the same form to limit the available positions in the selected team.
I managed to limit each team, so that it has a maximum of 9 players.
The idea is to have +-2 players on each position( 2 forward, 2 center, 2 back, 3 substitutes) for each team, if the spots are full the 'position' (ex. forward) option doesn't show up for the selected team.
Might be helpful : I am using foreign keys : (player -> one position_id, team -> many players)
My positions table is consisted of id, name and max and it is passed as $positions
I am trying to figure out a way, i am new to the subject so if you guys can help me, would be a great.
Or tell me if it isn't possible.
Thanks in advance.
<form action="/player" method="POST" enctype="multipart/form-data" class="form-control">
#csrf
...
<div class="mb-3">
<select name="team_id">
<option selected>Select Team</option>
#foreach ($teams as $team)
#if ($team->players->count() < 9)
<option value="{{ $team->id }}">{{ $team->name }}</option>
#endif
#endforeach
</select>
</div>
<div class="mb-3">
<select name="position_id">
<option selected>Select Position</option>
#foreach ($teams as $team)
#if ($team->id == ???)
#foreach ($positions as $position)
#if ($position->count() > $position->max)
<option value="{{ $position->id }}" disabled>{{ $position->name }}</option>
#else
<option value="{{ $position->id }}">{{ $position->name }}</option>
#endif
#endforeach
#endif
#endforeach
</select>
</div>
...
<button type="submit" class="btn btn-primary">submit</button>
</form>
```
I want to pass selected value from dropdown to be passed throw routing from the same blade file
Here is my blade code :
<div class="form-group col-md-2">
<label>#lang('site.son')</label>
#if(Auth::user()->shortsign == '--')
<select class="select2-size-lg form-control border-primary" style="width: 100%;" name="sign">
#foreach ($users as $user)
<option value="{{ $user->shortsign }}">{{ $user->shortsign }}</option>
#endforeach
</select>
#else
<select class="select2-size-lg form-control border-primary" style="width: 100%;" name="sign" readonly>
<option value="{{ Auth::user()->shortsign }}">{{ Auth::user()->shortsign }}</option>
</select>
#endif
</div>
<div class="form-actions right">
<i class="la la-search" aria-hidden="true"></i> #lang('site.search')
</div>
Now the code iam using pass the last value from database not selected value
you can try this by giving id to your anchor tag
<script type="text/javascript">
$('.select2-size-lg').change(function () {
var id = $(this).val();
document.getElementById("abc").href="{{ url('/salereports/report/' . id) }}";
});
</script>
I tried to save a parent_id of Category in Laravel 5.8 successfully, but I want to edit category now.
CategoryController.php
public function edit(Category $category)
{
return view('Admin.categories.edit', compact('category'));
}
public function update(CategoryRequest $request, Category $category)
{
$category->update($request->all());
return redirect(route('categories.index'));
}
edit.blade.php
<form action="{{ route('categories.update', $category->id) }}" method="post">
{{ method_field('PATCH') }}
{{ csrf_field() }}
#include('Admin.layouts.errors')
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" value="{{ old('name') ? : $category->name }}" id="name" name="name">
</div>
<div class="form-group">
<label for="parent_id">Sub Category</label>
<select class="form-control" id="parent_id" name="parent_id" data-live-search="true">
#foreach(\App\Category::all() as $category)
<option value="{{ $category->id }}" {{ trim($category->id) , $category->pluck('id')->toArray() ? 'selected' : '' }}>{{ $category->name }}</option>
#endforeach
</select>
</div>
<button type="submit" class="btn btn-primary">Save</button>
</form>
For example, I have the following categories in the database.
id name parent_id
1 Software 0
2 Hardware 0
3 Photoshop 1
4 CoredDraw 1
If a user, for example, selects CorelDraw for edit, open edit for it. Select the name input tag, write CorelDraw. Select the parent_id option tag select Software. Because this is parent_id is = 0.
You need to do some basic improvement in your code, Make a practice of fire query in the model or in the controller
Blade file is not for load the data from the database
Controller
public function edit(Category $category)
{
$allCategory = \App\Category::all();
return view('Admin.categories.edit', compact('category','allCategory'));
}
Blade
<select class="form-control" id="parent_id" name="parent_id" data-live-search="true">
<option value="">Select Parent</option>
#foreach($allCategory as $cate)
<option value="{{ $cate->id }}" {{ $category->id == $cate->id ? 'selected' : '' }}>{{ $cate->name }}</option>
#endforeach
</select>
Once the user starts typing different category name you need to reset the select box using any client side script
I use the basic registration form but I want to add a multiple select, a User can have multiple Skills and select them when he register.
My problem is that when I select multiples values Laravel only take the last one, so I search on the web for a solution and they say to add [] after my class name, so this is what I do :
<div class="form-group row">
<div class="col-md-6">
<?php $competences = \App\Competence::all(); ?>
<select name="competences[]" multiple="" class="form-control">
<option value="" selected disabled style="display:none">choose your school</option>
#foreach ($competences as $competence)
<option value="{{ $competence->id }}">{{ $competence->name }}</option>
#endforeach
</select>
</div>
</div>
But now, when I submit the form the page reload and stay in the register form, and the user is not register... I don't know what to do, anyone of you have a solution ?
Here is how I check the competences in my registerController
$competences = $data['competences'];
foreach ($competences as $comp){
$user->competences()->save(Competence::find($comp));
}
Use multiple="multiple" OR just multiple in blade.php:
<div class="form-group row">
<div class="col-md-6">
<?php $competences = \App\Competence::all(); ?>
<select name="competences[]" class="form-control" multiple>
<option value="" selected disabled style="display:none">choose your school</option>
#foreach ($competences as $competence)
<option value="{{ $competence->id }}">{{ $competence->name }}</option>
#endforeach
</select>
</div>
</div>
then in your controller, verify that you're actually getting the data in the form of an array by just printing $data
return $data;