Laravel : how to explode and showing multiple data in select2 - laravel

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

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

Predefined redundant values in view Laravel

I have here a simple modal that edits user privileges. I want to show the current value of the selected user first but it seems my code is redundant in nature as it repeats. These values are not in a separate table by the way. Is there any simpler way to do this in this view?
<div class="form-group col-md-4">
<label for="user_type">Privilege</label>
<select name="user_type" class="form-control" id="">
<?php $user_type = $user->user_type ?>
#if ($user_type == 'Admin')
<option value="Admin" selected>Admin</option>
<option value="Facilitator">Facilitator</option>
<option value="TeamLeader">TeamLeader</option>
<option value="ScrumLeader">ScrumLeader</option>
<option value="Member">Member</option>
#elseif ($user_type == 'Facilitator')
<option value="Admin">Admin</option>
<option value="Facilitator" selected>Facilitator</option>
<option value="TeamLeader">TeamLeader</option>
<option value="ScrumLeader">ScrumLeader</option>
<option value="Member">Member</option>
#elseif ($user_type == 'TeamLeader')
<option value="Admin">Admi n</option>
<option value="Facilitator">Facilitator</option>
<option value="TeamLeader" selected>TeamLeader</option>
<option value="ScrumLeader">ScrumLeader</option>
<option value="Member">Member</option>
#elseif ($user_type == 'ScrumLeader')
<option value="Admin">Admin</option>
<option value="Facilitator">Facilitator</option>
<option value="TeamLeader">TeamLeader</option>
<option value="ScrumLeader" selected>ScrumLeader</option>
<option value="Member">Member</option>
#else
<option value="Admin">Admin</option>
<option value="Facilitator">Facilitator</option>
<option value="TeamLeader">TeamLeader</option>
<option value="ScrumLeader">ScrumLeader</option>
<option value="Member" selected>Member</option>
#endif
</select>
</div>
I would suggest using the laravelcollective/html package (here)
You will then be able to write something like:
{{Form::select('user_type', $usertypearray, $user->user_type, ['class' => 'form-control']) }}
Where $usertypearray is a value => label array. For instance:
$usertypearray = ['admin'=> 'Admin', 'facilitator'=> 'Facilitator', 'teamleader'=> 'TeamLeader', 'scrumleader'=> 'ScrumLeader','member'=>'Member'];
(I put values in lowercase, but check if it's what the server is looking for)
You can looping on select tag like this
{{ Form::select('user_type',$user_type ,old('user_type'),['class' => 'form-control']) }}
Hope this helps :)

Laravel Edit Form - Select default selection

I'm creating an edit form in Laravel and I would like to select the selected (database) value in Select Dropdown. I'm doing it now as below. Is the right method or anything better is possible? I'm using Laravel 5.4.
<select class="form-control" id="type" name="type">
<option value="admin" #if($user->type==='admin') selected='selected' #endif>Admin</option>
<option value="telco" #if($user->type==='telco') selected='selected' #endif>Telco</option>
<option value="operator" #if($user->type==='operator') selected='selected' #endif>Operator</option>
<option value="subscriber" #if($user->type==='subscriber') selected='selected' #endif>Subscriber</option>
</select>
I would say that this is a normal approach.
If you want to make it more "friendly" it would take some overhead, for example using a #foreach loop to loop an array that would create all the options for the select, something like this :
$arr = ['admin','telco','operator','subscriber'];
#foreach($arr as $item)
<option value="{{ $item }}" #if($user->type=== $item) selected='selected' #endif> {{ strtoupper($item) }}</option>
#endforeach
You can also use ternary operator instead of if
<select class="form-control" id="type" name="type">
<option value="admin" {{($user->type ==='admin') ? 'selected' : ''}}> Admin </option>
<option value="telco" {{($user->type ==='telco') ? 'selected' : ''}}> Telco </option>
<option value="operator" {{($user->type ==='operator') ? 'selected' : ''}}> Operator </option>
<option value="subscriber" {{($user->type ==='subscriber') ? 'selected' : ''}}> Subscriber </option>
</select>

Select from dropdown always return first record?

I have a problem with dropdown, i always get first record. Any suggestion?
$category = $request->input('article_category');
<select class="form-control" name="article_category">
<option value="" disabled>--Please select article category</option>
#foreach ($categories as $category)
<option value="{{ $category->id }}">{{ $category->title }}</option>
#endforeach
Please make the following change...
<select class="form-control" name="article_category">
<option value="" disabled>--Please select article category</option>
#foreach ($categories as $category)
<option value="{{ $category->id }}" #if(request()->input('article_category') == $category->id){{ 'selected' }}#endif>{{ $category->title }}</option>
#endforeach
</select>
Your $category = $request->input('article_category') was getting overwritten by the foreach($categories as $category) written in foreach.
Also, you must add the selected keyword to mark the option as selected on page load.

Resources