Laravel 5 - Pre-populate a HTML select using database value and old - laravel

I am trying to use 'old' in a Laravel 5 app to pre-populate a select form on an edit route like this...
<select id="category" name="category" required>
<option value="">Select</option>
<option value="animal" #if (old('category') == "animal") {{ 'selected' }} #endif>Animal</option>
<option value="vegetable" #if (old('category') == "vegetable") {{ 'selected' }} #endif>Vegetable</option>
<option value="mineral" #if (old('category') == "mineral") {{ 'selected' }} #endif>Mineral</option>
</select>
This works well and keeps the selected option if a validation fails, but I am trying to make it work so that it pre-populates when the page first loads.
How do I determine if this is the first load of the edit page, or if it has reloaded after a validation failure? Or is there a better way to do this?

Lets imagine you have sent the category value as $category.
So in every <option> tag,
<option value="animal"
{{ old('category') == 'animal' ? ' selected' :
$category == 'anumal' ? ' selected' : '' }}>
Animal
</option>
This is what is going on there:
if there is an old value and its matching, select this,
else if the original value is matching select this,
else do nothing.

Use the second parameter of the old function with a default/initial value:
<option value="animal" #if (old('category', 'animal') == "animal") {{ 'selected' }} #endif>Animal</option>
The second parameter will be returned as the function result if an old value cannot be found in the flashed input.

Related

If old value then select old value of Select Box otherwise select value coming from Controller

I have a select box in the edit form, and everything is working fine, but the problem is when there is an old value, it always selects the value from the database. I think the issue is loop indexing. When I choose an option down from the original value, it selects the old value, but it retains the initial value when selecting an option above the selected value.
<select class="form-control
searchableSelect {{ $errors -> has('Cash_expense_Account') ? 'is-invalid' : '' }}"
name="Cash_expense_Account">
<option value="">Search...</option>
#for($i = 0; $i < count($Cash_Accounts); $i++)
<option value="{{ $Cash_Accounts[$i]['id'] }}"
{{ ( $Cash_Accounts[$i]['id'] == $expense->coa_sub_account_id) ? 'selected' : '' }}
{{ old('Cash_expense_Account') == $Cash_Accounts[$i]['id'] ? 'selected' : '' }}>
{{ $Cash_Accounts[$i]['acc_sub_name'] }}
</option>
#endfor
</select>
Limit the comparison to only use old value if it exists, like:
<option value="{{ $Cash_Accounts[$i]['id'] }}" {{ ( (old('Cash_expense_Account')??$Cash_Accounts[$i]['id']) == $expense->coa_sub_account_id) ? 'selected' : '' }}>
{{ $Cash_Accounts[$i]['acc_sub_name'] }}
</option>
the ?? operator checks if old value exists otherwise use one from the loop.

How to avoid checking every time, if variable is set in blade template? Laravel 8

I know about something like this:
{{ old('contents', $page->contents ?? null) }}
But what about more complicated cases, like checkboxes and selects?
<select id="custom_template" name="custom_template">
{{--default empty option, if nothing is selected yet--}}
<option label=" " {{ ($page->custom_template == null)? "selected" : "" }}></option>
#foreach($templates as $template)
<option value="{{ $template->id }}" {{ ($page->custom_template == $template->id)? "selected" : "" }}>{{ $template->name }}</option>
#endforeach
</select>
I need to avoid checking #isset($page) and also check old input. How can I do it in that select input?
You can use Type hinting to avoid the 'isset'.
Your controller
/**
* Show the form for creating a new resource.
*
* #param \App\Entities\Page $page
*
* #return \Illuminate\View\View
*/
public function create(\App\Entities\Page $page)
{
return view('page', compact('page'));
}
With using 'Type hinting' in create function will create empty collection of Page Entities. No need to check isset condition in view, $page->custom_template will give you null now, instead of error.
And your view, to check the old input.
<select id="custom_template" name="custom_template">
<option value="">Select Option</option>
#foreach($templates as $template)
<option value="{{ $template->id }}" {{ (old('custom_template', $page->custom_template) == $template->id) ? 'selected' : '' }}>{{ $template->name }}</option>
#endforeach
</select>
By using the above condition you can use same view for create and edit function.
Hope, this will solve your problem.
I'm not sure I quite understand what you're trying to do but this approach may help; provided you're using JQuery:
<script>
$(document).ready(function() {
#if($page->custom_template)
$('option[value="{{ $page->custom_template}}"]').prop("selected", true);
#endif
});
</script>

Laravel search-filter

I have a problem with passing the selected index to my controller via click.
If I manually change the index in the browser, it is working.
(http://localhost:3000/admin/users?user=&sortBy=5)
$sortOptions is the name of my 2d array in my controller.
sortDisplay is a field in my 2d array in my controller.
Am I missing something in my foreach loop?
<label for="sortBy">Sort by</label>
<select class="form-control" name="sortBy" id="sortBy">
#foreach($sortOptions as $index => $sortOptions)
<option value="{{$index}}" {{ (request()->sortBy == $index ? 'selected' : '') }}>
{{$sortOptions["sortDisplay"]}}
</option>
#endforeach
</select>
Use jQuery to submit form when value changed
So your page will refresh and you will get what you want
In the foreach loop, you are assigning the same variable name as the variable you are iterating. In your case, after the first loop, you re-instantiate the $sortOptions variable with the content of the first index of $sortOptions.
#foreach($sortOptions as $index => $sortOption) // <-- $sortOption, not $sortOption(s)
<option value="{{$index}}" {{ (request()->sortBy == $index ? 'selected' : '') }}>
{{ $sortOption["sortDisplay"] }}
</option>
#endforeach

how to dynamically option the selected in html just using php?

I'm trying to dynamically the select option selected just using php in laravel but I'm getting this error:
Use of undefined constant selected - assumed 'selected' (this will
throw an Error in a future version of PHP) (View:
C:\xampp\htdocs\laralast\resources\views\view.blade.php)
below is my view blade
<select class="form-control" name="assign_to" id="assign_to">
<option selected disabled>Select support</option>
#foreach($supports as $support)
<option value="{{$support->id}}" {{($ticket->assign_by == $support->id ? selected : '')}}>{{$support->fullname}}</option>
#endforeach
</select>
Can you help me with this.
You should quote your string (I mean you should quote the selected):
<option value="{{$support->id}}" {{($ticket->assign_by == $support->id ? 'selected' : '')}}>{{$support->fullname}}</option>
Your laravel is understanding that you will print the value of selected constant (since no dollar-sign $, no string quotation '' "") when the condition is true.
Please Check This use if & else
<select class="form-control" name="assign_to" id="assign_to">
<option selected disabled>Select support</option>
#foreach($supports as $support)
#if($ticket->assign_by == $support->id)
<option value="{{$support->id}}" selected>{{$support->fullname}}
</option>
#else
<option value="{{$support->id}}">{{$support->fullname}}</option>
#endforeach
</select>

How to use Blade to append tags

I have Blade view with some dropdowns. Also, I am passing data to this view through my controller.
<select name="location">
<option value="some-value1">Some Label1</option>
<option value="some-value2">Some Label2</option>
</select>
and I have property $model->location. How do I append selected to the right option? Is it even possible to do that?
You can use a ternary operator in a Blade slot, for example:
<option value="value" {{ $model->location == 'value' ? 'selected' : '' }}>Label</option>
The selected option will only be displayed when your condition ($model->location == 'value') is true

Resources