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')
Related
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
On my test site I have a search by cities and categories, which are selected using a select
<div class="form-group">
<select class="selectpicker" name="sCity">
#foreach($cities as $city)
<option value="{{$city->slug}}">{{$city->name}}</option>
#endforeach
</select>
</div>
<div class="form-group">
<select class="selectpicker" name="sCat">
#foreach($categories as $cat)
<option value="{{$cat->slug}}">{{$cat->name}}</option>
#endforeach
</select>
</div>
And such a method for searching
public function search(Request $request)
{
$result = DB::table('ads')
->where('city_slug', $request->sCity)
->orWhere('category_slug', $request->sCat)
->get();
dd($result);
}
If you search for one select, then the search is correct, but if you set values to two selections at once, then only the first is triggered, and the second is ignored. How can you fix this?
Try this:
$result1 = DB::table('ads')
->where('city_slug', $request->sCity)
->get();
$result2 = DB::table('ads')
->where('category_slug', $request->sCat)
->get();
$result = $result1->merge($result2):
you could do something like this
in your view
<div class="form-group">
<select class="selectpicker" name="sCity">
<option value=0>please select one</option>
#foreach($cities as $city)
<option value="{{$city->slug}}">{{$city->name}}</option>
#endforeach
</select>
</div>
<div class="form-group">
<select class="selectpicker" name="sCat">
<option value=0>please select one</option>
#foreach($categories as $cat)
<option value="{{$cat->slug}}">{{$cat->name}}</option>
#endforeach
</select>
</div>
and in your controller
$city_slug='';
$category_slug='';
if($request->sCity!=0)
{
$city_slug=' city_slug='.$request->sCity;
}
if($request->sCat!=0)
{
$category_slug=' or category_slug='.$request->sCat;
}
$result = DB::table('ads')
->whereRaw($city_slug.$category_slug)
->get();
dd($result);
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
I am trying to get some data (all filenames) from files table in dropdown but it gives error: Undefined variable: files
Controller:
public function show($id)
{
$data = File::findOrFail($id);
$files = \DB::table('files')->get();
return view('userhome', compact('data', 'files'));
}
Blade template:
<div class="form-group">
<label class="text-left">Select Record</label>
<select name="parent_id">
<option value="">Select Record</option>
#foreach ($files as $filename)
<option value="{{ $filename->id }}">{{ $files->filename }}</option>
#endforeach
</select>
</div>
web.php: https://ibb.co/xMswcTG
What causes this error?
you return userhome.blade.php into your controller but try to get $files and $data variables into home.blade.php
change
return view('userhome', compact('data', 'files'));
to
return view('home', compact('data', 'files'));
all must be working ;)
You need to use "$filename->" object instead of "$files->" object in options tag.
Please update the blade with below code.
<div class="form-group">
<label class="text-left">Select Record</label>
<select name="parent_id">
<option value="">Select Record</option>
#foreach ($files as $filename)
<option value="{{ $filename->id }}">{{ $filename->filename }}</option>
#endforeach
</select>
</div>
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.