Livewire dependent dropdown, multiple row in a form - laravel

i have a form than can submit multiple rows using livewire. I have school level and subjects model. School level has many subjects. Subjects belongs to School level. I want to make dependent dropdown. When i select a school level, the subjects related to the school level will be loaded. If i only have one row(one entry). it would not be a problem, but when i add new row, the subjects select field will follow the first row value. How do i load subjects so that it is unique to its own row?
School Level:
<label class="required form-label">School Level</label>
<select wire:model="studentForms.{{ $index }}.level" name="level" class="form-select" required>
<option value="" selected>--Select School Level--</option>
#foreach ($schoolLevels as $schoolLevel)
<option class="mb-2" value="{{ $schoolLevel->id }}">{{ $schoolLevel->name }}</option>
#endforeach
</select>
Subjects:
<select wire:model="studentForms.{{ $index }}.subject" name="subject" class="form-select" data-placeholder="Select Subjects" required>
<option value="" selected>--Select Subject--</option>
#foreach ($subjects as $subject)
<option value="{{ $subject->id }}">{{ $subject->name }}</option>
#endforeach
</select>
Livewire Class:
public function mount()
{
$this->subjects = collect();
}
public function render()
{
return view('livewire.form', [
'schoolLevels' => SchoolLevel::has('subjects')->get(),
]);
}
public function updated($value, $key)
{
$this->subjects = Subject::where('school_level_id', $key)->get();
}
Livewire Class

Related

Getting name and id value from select Laravel livewire

I need a little help. I want to save also $item->name from the option to the Category database in the name column. Thanks for the help.
My blade code:
<label>Select a Category</label>
<select class="select2 form-control" for="cateId" wire:model.lazy="cateId">
<option value="">Select a Category</option>
#foreach ($categorylist as $item)
<option value="{{ $item->id }}">{{ $item->name }}</option>
#endforeach
</select>
my livewire controller:
public $name;
public function submit()
{
Category::create([
'name' => $this->$item->name,
]);
}
my Category model:
protected $table = "category";
protected $fillable = [
'name',
];
Just change option value like this:
<label>Select a Category</label>
<select class="select2 form-control" for="cateId" wire:model.lazy="catName">
<option value="">Select a Category</option>
#foreach ($categorylist as $item)
<option value="{{ $item->name }}">{{ $item->name }}</option>
#endforeach
</select>
and get it from $request->input('catName')

how to insert multi value data to database in laravel?

I want to store multiple data to database because i have 2 select option with same name. i've following some tutorial from google and youtube but i still can't figured out how to do it.
Table structure:
id | nama_mapel | guru_id | kode_mapel | ki_kd_id |
I have view like this, there's 2 select option with same name:
<input type="text" name="nama_mapel[]">
<input type="text" name="kode_mapel[]">
<select type="text" name="guru_id[]">
<option value disable>Pilih Guru</option>
#foreach ($guru as $item)
<option value="{{ $item->id }}">{{ $item->nama_guru }}</option>
#endforeach
</select>
<select name="ki_kd_id[]">
<option value disable>Pilih Kompetensi Dasar</option>
#foreach ($komp_dasar as $kd)
<option value="{{ $kd->id }}">{{ $kd->kompetensi }}</option>
#endforeach
</select>
<select type="text" name="ki_kd_id[]">
<option value disable>Pilih Kompetensi Inti</option>
#foreach ($komp_inti as $ki)
<option value="{{ $ki->id }}">{{ $ki->kompetensi }}</option>
#endforeach
</select>
and this is my controller:
public function store(Request $request)
{
$nama_mapel = $request->nama_mapel;
$guru_id = $request->guru_id;
$kode_mapel = $request->kode_mapel;
$ki_kd_id = $request->ki_kd_id;
foreach ($nama_mapel as $row => $key){
$data= [
'nama_mapel' => $nama_mapel[$row],
'guru_id' => $guru_id[$row],
'kode_mapel' => $kode_mapel[$row],
'ki_kd_id' => $ki_kd_id[$row],
];
DB::table('mapel')->insert($data);
}
return redirect()->back();
}
Any help would be very appreciated, and sorry for my bad english.
I have found out how to do this.
For view it like this, only use array [] in ki_kd_id name. The reason is because the others is same. I have duplication for all fields except ki_kd_id.
<input type="text" name="nama_mapel">
<input type="text" name="kode_mapel">
<select type="text" name="guru_id">
<option value disable>Pilih Guru</option>
#foreach ($guru as $item)
<option value="{{ $item->id }}">{{ $item->nama_guru }}</option>
#endforeach
</select>
<select name="ki_kd_id[]">
<option value disable>Pilih Kompetensi Dasar</option>
#foreach ($komp_dasar as $kd)
<option value="{{ $kd->id }}">{{ $kd->kompetensi }}</option>
#endforeach
</select>
<select type="text" name="ki_kd_id[]">
<option value disable>Pilih Kompetensi Inti</option>
#foreach ($komp_inti as $ki)
<option value="{{ $ki->id }}">{{ $ki->kompetensi }}</option>
#endforeach
</select>
and for the controller, it will be like this:
public function store(Request $request)
{
foreach ($request->ki_kd_id as $value){
if ($value) {
DB::table('mapel')->insert([
'nama_mapel' => $request->nama_mapel,
'guru_id' => $request->guru_id,
'kode_mapel' => $request->kode_mapel,
'ki_kd_id' => $value,
]);
}
}
return redirect()->back();
}
Use foreach to loop the request based on how many ki_kd_id that the request have. and then for the value just use $value as representative of ki_kd_id

Laravel : how to explode and showing multiple data in select2

I am using select2 and i want to showing multiple data from database in edit page
#foreach( $userLanguages as $userLanguageTitle)
<option value="{{$user->languages}}">{{$userLanguageTitle}}</option>
#endforeach
But showing me
<select name="languages[]" class="select2 form-control" multiple="multiple" required>
<option value="English,Persian,German" >English</option>
<option value="English,Persian,German" >Persian</option>
<option value="English,Persian,German" >French</option>
<option value="English,Persian,German" >German</option>
<option value="English,Persian,German" >Spanish</option>
<option value="English,Persian,German" >Turkish</option>
<option value="English,Persian,German" >Italian</option>
</select>
I want show selected data in element Like the example below
<select name="languages[]" class="select2 form-control" multiple="multiple" required>
<option value="English" selected >English</option>
<option value="Persian" selected >Persian</option>
<option value="French">French</option>
<option value="German" selected >German</option>
<option value="Spanish" >Spanish</option>
<option value="Turkish" >Turkish</option>
<option value="Italian" >Italian</option>
</select>
Model
public static function getUserlanguages()
{
return [
self::ENGLISH => 'English',
self::PERSIAN => 'Persian',
self::FRENCH => 'French',
self::GERMAN => 'German',
self::SPANISH => 'Spanish',
self::TURKISH => 'Turkish',
self::ITALIAN => 'Italian',
];
}
controller
public function edit(Request $request, $id)
{
$user = User::findOrFail($id);
$userLanguages = User::getUserlanguages();
return view('backend.users.edit', compact('user', 'userLanguages',));
}
Try this, assuming $user->languages is a relation in User model and there is column name in languages table
#foreach($userLanguages as $userLanguageTitle)
<option value="{{$userLanguageTitle}}" {{ (in_array($userLanguageTitle, $user->languages()->pluck('name')->toArray()) ? 'selected' : '') }}>{{$userLanguageTitle}}</option>
#endforeach
<option value="{{$user->languages}}">{{$userLanguageTitle}}</option>
Since you edited the code above, I assume that $user->languages returns all Languages of an User. So it returns either a Collection or an Array but not a single Language item. So you can simply use value="{{$userLanguageTitle}}" to display the name of the language or you use the key of it like that:
#foreach( $userLanguages as $key => $userLanguageTitle)
<option value="{{$key}}">{{$userLanguageTitle}}</option>
#endforeach

How to convert and display the value from the database in laravel

I want do display outlet name as "Food" if the value in the database is "FC" and display "Express" if the value in the database is "EX" in my for each drop down.
Here's my controller
$outlet_name = StoreFC::select('loc_type')
->groupBy('loc_type')
->orderBy('loc_type', 'ASC')
->get();
$district_name = StoreFC::select('location')
->groupBy('location')
->orderBy('location', 'ASC')
->get();
return view('test.store-fc', compact('outlet_name','district_name'));
Here's my view
<div class="form-group">
<select name="filter_district" id="filter_district" class="form-control" required>
<option value="">Select Location</option>
#foreach ($district_name as $district)
<option value="{{ $district->location }}">{{ $district->location }}</option>
#endforeach
</select>
</div>
<div class="form-group">
<select name="filter_outlet" id="filter_outlet" class="form-control" required>
<option value="">Select Outlet</option>
#foreach($outlet_name as $outlet)
<option value="{{ $outlet->loc_type }}">{{ $outlet->loc_type }}</option>
#endforeach
</select>
</div>
Here's my model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class StoreFC extends Model
{
protected $table = 'store_foodcity';
}
This is how it show now in the drop down
You need to create custom getters in model StoreFC.
[https://laravel.com/docs/5.8/eloquent-mutators#defining-an-accessor][1]
The best thing U can do is to create a new column with the full name "Food" for "FX", otherwize.... you have to modify that mutator every time U change something in database.

show data select option by user in laravel

I want to show Project Data in select option by assign User login.
this my form select option
<div class="form-group">
<label for="">Project *</label>
<select class="form-control select2" style="width: 100%;" name="project_id">
<option value="project_id" selected>Select One</option>
#foreach($projects as $id => $project)
<option value="{{$id}}">{{$project}}</option>
#endforeach
</select>
</div>
this my controller to pluck project name by assign user
$projects = UserProjects::pluck('project_id', 'user_id');
and I use Many To Many Relation.
in my User Model
public function projects()
{
return $this->belongsToMany(Project::Class, 'user_projects');
}
in my Project model
public function users()
{
return $this->belongsToMany(User::class, 'user_projects');
}
and in my table pivot user_projects it just project_id and user_id. what should i write in my select option in form.?
In many to many relation you don't have to define a Model for the intermediate table (user_projects).
Controller
$user_projects = auth()->user()->projects;
View
<div class="form-group">
<label for="">Project *</label>
<select class="form-control select2" style="width: 100%;" name="project_id">
<option value="project_id" selected>Select One</option>
#foreach($user_projects as $user_project)
<option value="{{$user_project->id}}">{{$user_project->name}}</option>
#endforeach
</select>
</div>

Resources