Laravel select old and new value as selected - laravel

Not sure exactly how to ask this question. But how do you select the old value after you fetched the value from the database? When you click on edit, you fetch the database result. But if something goes wrong, I want the value selected that the user inserted.
This is what works if you want to add a value.
<select name="category_id" class="form-control" id="category">
#foreach($category as $cat)
<option value="{{$cat->id}}"
{{ old('category_id', $cat->id) == $subCategory->category_id ? "selected" : "" }}>
{{ $cat->category_en }} | {{ $cat->category_de }}</option>
#endforeach
</select>
However, when I press update, it doesn't use the old selected value, but it automatically goes to the first option in the foreach.

use optional() helper along with the old() helper to handle this kind of situation.
<option value="{{ $cat->id }}" #if (old('category_id', optional($subCategory)->category_id) == $cat->id) selected #endif>{{ $cat->category_en }} | {{ $cat->category_de }}</option>
what this means is when it gets old input value null, it will use the object value to select an item. when there is old input value it will select the old input value. you can omit the optional helper though, it is useful when you are using the same form for create and update.

<select name="category_id" class="form-control" id="category">
#foreach($category as $cat)
<option value="{{$cat->id}}"
#if($cat->id == $subCategory->category_id) ? selected : '' #endif>
{{ $cat->category_en }} | {{ $cat->category_de }}</option>
#endforeach
</select>

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;

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>

Add attribute to first item of array in Laravel

I have a select box and want to get the options of an array.
<select class="form-control select-search" name="user_select_crypto"
id="user_select_crypto">
#foreach($currencies as $currency)
<option value="{{ $currency->id }}">{{ $currency->crypto->name }}</option>
#endforeach
</select>
Now, I want to first item add the selected attribute, how can I do this?
You can use one of 2 things: Either $index => $val approach as classic foreach.
#foreach($currencies as $i => $currency)
<option value="{{ $currency->id }}" {{ $i == 0 ? 'selected' : ''}}>{{$currency->crypto->name}}</option>
#endforeach
Also as of more Laravel way, there is $loop variable you can access inside blade in #foreach
#foreach($currencies as $i => $currency)
<option value="{{ $currency->id }}" {{ $loop->first ? 'selected' : ''}}>{{$currency->crypto->name}}</option>
#endforeach
Of course, you can translate {{ $loop->first ? 'selected' : ''}} into:
#if ($loop->first) 'selected' #endif
A better way to deal with such issues is to add a default <option></option>;
<select class="form-control select-search" name="user_select_crypto" id="user_select_crypto">
<option value="" selected disabled >Select Currency</option>
#foreach($currencies as $key => $currency)
<option value="{{ $currency->id }}">{{$currency->crypto->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.

Populate select dropdown if returning with errors

As with my text fields, if an error is returned on the view, I want to populate the select elements with the previously submitted data, like: value="{{ old('element_name_here') }}".
Current view:
<select class="form-control" id="input" name="{{ $questionDetail->question_number }}" required="required">
<option disabled selected value> -- Select an option --</option>
#foreach($answers as $answer)
<option value="{{ $answer->value }}">{{ $answer->question }}</option>
#endforeach
</select>
How can I implement it into the above scenario, as there doesn't seem to be a place where I can add a value without overwriting one? Thank you.
<option
value="{{ $answer->value }}"
{{ $answer->value == old(questionDetail->question_number) ? 'selected' : '' }}>
{{ $answer->question }}
</option>
You can remove 'selected' from the first option. It will be selected by default because it's the first. if old() exists, then it will do the check.
You need to mark previous value as selected so something like this should work:
<select class="form-control" id="input" name="{{ $questionDetail->question_number }}" required="required">
<option disabled #if(!old($questionDetail->question_number)) selected #endif value> -- Select an option --</option>
#foreach($answers as $answer)
<option value="{{ $answer->value }}" #if(old($questionDetail->question_number) == $answer->value) selected #endif>{{ $answer->question }}</option>
#endforeach
</select>
Probably when working a lot with forms it's a good idea to use https://laravelcollective.com/docs/5.3/html (branch 5.4)

Resources