How get old value on select in laravel blade? - laravel

i have filter form using select dropdown , but i cant get this old value after i submit this filter like input like type text . someone can help ?
<div class="form-group">
<label>Status</label>
<select class="form-control select2 select2-hidden-accessible" style="width: 100%;" data-select2-id="1" tabindex="-1" aria-hidden="true"
name="user_id" id="user_id" required
>
#foreach($unit as $id => $nama_unit )
<option value="{{ $id }}">{{ $nama_unit }}</option>
#endforeach
</select>
<div class="help-block with-errors"></div>
</div>
i add my function controller :
public function search_filter_alkes(Request $request)
{
$unit = User::where('roles_id' , 1)->pluck('nama_unit', 'id');
$user_id = $request->user_id;
$alat = Alat::with('users')->where('user_id',$user_id)
->where('jenis', 'Alkes')->
get();
session()->put('user_id',$user_id);
return view('sarpras.alkes',['user_id' => $user_id , 'unit' => $unit,'alat' => $alat ])
->with('user_id', $user_id)
;
}

as per your code use this and make sure you use withInput()
return redirect()->back()->withErrors($validator)->withInput();
<div class="form-group">
<label>Status</label>
<select class="form-control select2 select2-hidden-accessible" style="width: 100%;" data-select2-id="1" tabindex="-1" aria-hidden="true"
name="user_id" id="user_id" required
>
#foreach($unit as $id => $nama_unit )
<option value="{{ $id }}" {{ old('user_id') == $id ? "selected" :""}}>{{ $nama_unit }}</option>
#endforeach
</select>
<div class="help-block with-errors"></div>
</div>
EDITED
When you use redirect() after post method you have to use withInput() as i mention above example
return redirect('route')->withInput();
when you use view() you have to pass data like as array like your code then use same variable name
return view('sarpras.alkes',['user_id' => $user_id , 'unit' => $unit,'alat' => $alat ])
<option value="{{ $id }}" {{ $user_id == $id ? "selected" :""}}>{{ $nama_unit }}</option>

You wil have to compare old value with input key. So replace the below code inside your for loop
#if (Input::old('user_id') == $id)
<option value="{{ $id }}" selected>{{ $nama_unit }}</option>
#else
<option value="{{ $id }}">{{ $nama_unit }}</option>
#endif

You have multiple options:
<select name="tags[]" class="form-control select-tag" multiple>
#foreach($tags as $tag)
<option value="{{$tag->id}}" {{in_array($tag->id, old("tags") ?: []) ? "selected": ""}}>{{$tag->name}}</option>
#endforeach
</select>
OR
<select name="gender" class="form-control" id="gender">
<option value="">Select Gender</option>
<option value="M" #if (old('gender') == "M") {{ 'selected' }} #endif>Male</option>
<option value="F" #if (old('gender') == "F") {{ 'selected' }} #endif>Female</option>
</select>

This worked for me in laravel 8:
<select class="form-control" id="gender" name="gender" required>
<option value="">Select gender</option>
<option value="M" #if (old('gender') == "M") {{ 'selected' }} #endif>Male</option>
<option value="F" #if (old('gender') == "F") {{ 'selected' }} #endif>Female</option>
<option value="O" #if (old('gender') == "O") {{ 'selected' }} #endif>Other</option>
</select>

Related

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

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>

How can I show the old value in option fields in Laravel?

I want to show the old select value in this select field when I want to edit. How can I show my old value in these fields?
https://github.com/shakibzaman/book-library-laravel
you can show my GitHub repo-
Here is my rules edit page:
<div class="form-group {{ $errors->has('name') ? 'has-error' : '' }}">
<label for="chap_id">{{ trans('cruds.chapters.title_singular') }}*</label>
<select name="chap_id" id="chap_id" class="form-control select2">
#foreach($chapters as $id => $chapter)
<option value="{{ $id }}">{{ $chapter }}</option>
#endforeach
</select>
#if($errors->has('name'))
<em class="invalid-feedback">
{{ $errors->first('name') }}
</em>
#endif
<p class="helper-block">
{{ trans('cruds.permission.fields.title_helper') }}
</p>
</div>
editController
public function edit($id)
{
if (!Gate::allows('users_manage')) {
return abort(401);
}
$rule = Rule::find($id);
$chapters = Chapter::all()->pluck('name', 'id')->prepend(trans('global.pleaseSelect'), '');
return view('admin.rules.edit', compact('rule', 'chapters'));
}
You can use the old() function to get the old value of a field. This function requires one input and that is the name of the field: old('chap_id').
You could use this one:
#foreach($chapters as $id => $chapter)
#if(null !== old('chap_id') && old('chap_id') === $id)
<option value="{{ $id }}" selected>{{ $chapter }}</option>
#else
<option value="{{ $id }}">{{ $chapter }}</option>
#endif
#endforeach
Or the shorter one:
#foreach($chapters as $id => $chapter)
<option value="{{ $id }}" {{ old('chap_id') === $id ? 'selected' : '' }}>{{ $chapter }}</option>
#endforeach

How to use isset in select option in laravel form

How to use isset if i use select option?
Please help.
This is how i use to get my data for normal insert.
<div class="form-group">
<label>Zone Name</label>
<input type="text" name="name" class="form-control" value="{{ old('name',isset($zone) ? $zone->name : '') }}" placeholder="Name">
</div>
But i don't know how to use isset in select option.
<div class="form-group">
<label>Select Country</label>
<select class="form-control" name="country_id">
<option value="" selected>Please Select</option>
#if ($country->count())
#foreach($country as $c)
<option value="{{ $c->country_id }}" {{ $selectCountry == $c->country_id ? : '' }}>{{ $c->name }}</option>
#endforeach
#endif
</select>
</div>
This is my Controller
public function edit(Request $request, $id){
$zone = Zone::find($id);
$country = Country::all();
$selectCountry= Country::first()->country_id;
if(!$zone){
return redirect('/');
}
return view('zone.edit',['zone' => $zone, 'country'=>$country, 'selectCountry'=>$selectCountry]);
}
my expected result is when i press on edit button, it will show the previous selected result.
You can write
<select class="form-control" name="country_id">
<option value="" {{ !isset($selectCountry) ? 'selected' : '' }}>Please Select</option>
#if ($country->count())
#foreach($country as $c)
<option value="{{ $c->country_id }}" {{ (isset($selectCountry) && $selectCountry == $c->country_id) ? 'selected' : '' }}>{{ $c->name }}</option>
#endforeach
#endif
</select>

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>

Resources