Setting selected option in laravel from database - laravel

I have a problem here, I want to make an edit form, I want the province that appears in the edit form to be the province selected from the database, here my code
<select name="provinsi" id="provinsi" class="form-control">
<option value="">Pilih Provinsi</option>
#foreach ($province as $prov)
<option value="{{$prov->name}}">{{$prov->name}}</option>
#endforeach
</select>
here my controller code
public function ShowSantri(Request $request,$id)
{
$province = DB::table('provinces')->get();
$profiles = Profile::find($request->id);
return view('admin/modal/edit_santri',compact('profiles','province'));

You can try like this:
<select name="provinsi" id="provinsi" class="form-control">
<option value="">Pilih Provinsi</option>
#foreach ($province as $prov)
<option value="{{$prov->name}}"
#if ($profiles['provName'] == $prov->name)
selected
#endif>
{{$prov->name}}
</option>
#endforeach
</select>
Just I'm not sure what is name in your $profiles for province. But I think that is what you need.
Here and here are some examples.
Good luck!

Use or change this code based on your needs:
<option value="{{$prov->name}}" {{ $prov->name == $profiles->prov_id ? "selected" : "" }}>{{$prov->name}}</option>
I think the $prov->name should be $prov->id, if not, you can change $profiles->prov_id to $profiles->prov_name. It depends on your database structure.

Related

selected option laravel livewire

I have a dropdown and in edit page I want to show old input. I use this code but it still shows "please select". Where is the problem?
<select wire:model.defer="form.department" id="form.department" class="tf-input">
<option value="null" selected disabled>{{ __('Please select') }}</option>
#foreach($this->departments as $department)
<option {{ $form->department_id == $department->id ? 'selected' : '' }} value="{{ $department->id }}" >{{ $department->name }}</option>
#endforeach
</select>
Livewire does not listen to selected attributes in HTML when you are using wire:model, because it will overwrite it with the value from the component. Therefor, remove it entirely.
<select wire:model.defer="form.department" id="form.department" class="tf-input">
<option value="null" disabled>{{ __('Please select') }}</option>
#foreach ($this->departments as $department)
<option value="{{ $department->id }}" wire:key="department-{{ $department->id }}">{{ $department->name }}</option>
#endforeach
</select>
As you can see, I removed the parts that adds selected, but I also added a wire:key to the options - this is because the elements are rendered in a loop, and will help Livewire keep track of all elements on a page.
To set a value that is bound through wire:model, you need to set the value in the component. So in the mount() method, you can do
$this->form['department'] = $department->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

show old data of `select` element in blade laravel 7

That's code showing deliverer names and if one selected I got his id on db table
<select name="deliverer_id" class="selectpicker form-control">
#foreach($dvs as $dv)
<option value="{{$dv->id}}"> {{ $dv->name }} </option>
#endforeach
</select>
I wanna show the old name of deliverer_id. I try before #if ...
You will need to compare your value in foreach loop with the old submitted value. For that Laravel provides a method to retreive old value from request $request->old('deliverer_id');
Or you can do it in your blade like this
<select name="deliverer_id" class="selectpicker form-control">
#foreach($dvs as $dv)
<option value="{{$dv->id}}" #if($dv->id == old('deliverer_id')) selected #endif > {{ $dv->name }} </option>
#endforeach
</select>
I foud the solution
#foreach($dvs as $dv)
<option value="{{ $dv->id}}"{{ old('deliverer_id', $order->deliverer_id) == $dv->id ? 'selected' : '' }} > {{$dv->name}}
</option>
#endforeach
I'm not sure what you mean but it seems that you want to select a default option based on specific condition in that case you can do as in the following:
<select name="deliverer_id" class="selectpicker form-control">
#foreach($dvs as $dv)
<option value="{{$dv->id}}" {{$dv->id == $old_deliverer_id ?? "selected" : ""}}> {{ $dv->name }}</option>
#endforeach
</select>

how to fix this category select method

Hi I'm just trying to make category for my movie blog and its working fine, but the editing category must be selected right.
So I did something like this:
//fetching all categories
#foreach($categories as $category)
//start option
<option
//fetching current movie categories
#foreach($movie->categories as $cat)
//matching is this category match with
#if($category->name === $cat->name)
//if match selected method works
selected="selected"
#endif
#endforeach
>{{ $category->name }}
</option>
#endforeach
and its working fine but i thinks its a wrong way
can u suggest me good way to do this
thanks
check this:
#if(in_array( $category->name ,$movie->categories->pluck('name'))
{{'selected' }}
#endif
if give you a type arg2 must be an array error. add a toArray() after the pluck methods.
Thats how I do it.
<select class="form-control" name="category_id">
#foreach($categories as $category)
<option value="{{$category->id}}" {{ isset($gallery) && $category->id == $gallery->category_id?'selected':''}}>{{$category->name}}</option>
#endforeach
</select>
You can use ternary operator instead of using if().
If you have many to many relationship then you can try
<select class="form-control" name="category_id">
#foreach($categories as $category)
<option value="{{$category->id}}" {{$movie->categories->contains('name', $category->name )?'selected':'' #endif >{{$category->name}}</option>
#endforeach
</select>
Hope this helps.

Laravel 5 issue with select drop down

The code for the controller:
public function index()
{$items = all_list::where('list_code', 'ABC-U47')->get();
//return $items;
return view('partials.curlist', compact('items'));
I know this works, I tested it with the return $items statement.
The code for the blade view:
</div><div class="form-group">
<select class="form-control" name="item_id">
#foreach($items as $item)
<option value="{{ $item->list_data }}"></option>
#endforeach
</select>
</div>
The view just shows and empty box. I know this is something very simple but, for the life of me I can't get it to work. Any help would be appreciated. Thanks in advance.
You need to have a text value for your option tag. Like this:
<option value="{{ $item->list_data }}">{{ $item->list_data }}</option>

Resources