How to use isset in select option in laravel form - laravel

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>

Related

topic.blade.php cannot fetch value->id, but it works with value->name

I got the code snippet,
<div class="form-group">
<label for="category-id-field">Category</label>
<select class="form-control" name="category_id" id="category-id-field">
<option value="" hidden disabled {{ $topic->id ? '' : 'selected' }}>Choose your category</option>
#foreach ($categories as $value)
#php
$selected = $topic->category_id === $value->id ? 'selected' : '';
#endphp
<option value="{{ $value->id }}" {{ $selected }}>{{ $value->name }}</option>
#endforeach
</select>
</div>
And it shows like below, as you can see $value->name works but $value->id just shows up zero.
<div class="form-group"><label for="category-id-field">Category</label>
<select name="category_id" id="category-id-field" class="form-control">
<option value="" selected="selected">Choose your category</option>
<option value="0">statement</option>
<option value="0">life</option>
<option value="0">work</option>
<option value="0">study</option>
</select>
</div>
Versions:
laravel/framework: "^7.0"
Platform: macos catalina
mysql: 8.0.19
Some more background:
Things I did, the id in the categories table used to be integer, I changed it to VARCHAR(30) and added it as the foreign key to table topics->category_id.
If anyone happens to have some experience on this one, please point out the error. Many thanks.

How get old value on select in laravel blade?

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>

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 Get value selected from database to edit

Let's say that I select a field the record is added to my database and my form, but when I want to change this field that I selected by another the first field is always by default, it does not return the selected field
Here was my code of create
<select name="father_id" class="form-control" required>
<option value="">--selectionner le parent svp --</option>
#foreach($fathers as $father)
<option value="{{ $eleve->father_id }}" >{{ $father->nom }}
{{ $father->prenom }}</option>
#endforeach
</select>
this is my edit form i tried this code
<select name="father_id">
#foreach($fathers as $father)
<option value="{{ $father->id }}"#if(old($father->father_id) ==
"father_id") selected #endif >{{ $father->nom }} {{ $father->prenom }}
</option>
#endforeach
</select>
my controller
public function update(Request $request, $id)
{
$note = Note::findOrFail($request->note_id);
$note->update($request->all());
session()->flash('success','Cet éléve a été modifié avec succés');
return redirect()->back();
}
the route
Route::resource('eleves','EleveController');
please help I am really stuck in this error
try it.
<select name="father_id">
#foreach($fathers as $father)
<option value="{{ $father->id }}" {{ old('father_id', '') === $father->id ? 'selected' : '' }}>
{{ $father->nom }} {{ $father->prenom }}
</option>
#endforeach
</select>

How to show default value in select option (parent_id)?

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

Resources