Setting Selected Option in Laravel Form

Setting selected option in laravel form

use this package and check the docs:

https://laravelcollective.com/docs/5.2/html#drop-down-lists

form you html , you need use this mark

{!! Form::select('size', array('L' => 'Large', 'S' => 'Small'), 'S'); !!}

Add default value to select list in Laravel form::select

You can use array_merge like this:

{{
Form::select(
'myselect',
array_merge(['' => 'Please Select'], $categories),
$myselectedcategories,
array(
'class' => 'form-control',
'id' => 'myselect'
))
}}

Alternatively you can set the placeholder somewhere before the select:

$categories[''] = 'Please Select';

Update

To add the disabled attribute you can try this: (untested)

{{
Form::select(
'myselect',
array_merge(['' => ['label' => 'Please Select', 'disabled' => true], $categories),
$myselectedcategories,
array(
'class' => 'form-control',
'id' => 'myselect'
))
}}

Getting selected option on edit form in laravel

you can compare $order->quantity with option value to add selected attribute

<select class="form-control" id="type" name="quantity">
<option value="">Select Quantity</option>
<option value="1" {{ $order->quantity == 1 ? 'selected' : '' }}>1</option>
<option value="2" {{ $order->quantity == 2 ? 'selected' : '' }}>2</option>
<option value="3" {{ $order->quantity == 3 ? 'selected' : '' }}>3</option>
<option value="4" {{ $order->quantity == 4 ? 'selected' : '' }}>4</option>
<option value="5" {{ $order->quantity == 5 ? 'selected' : '' }}>5</option>
</select>

for user compare $user->id with $order->user_id (or something else according your data)

<select class="form-control" id="type" name="user_id">
@foreach($users as $user)
<option value="{{ $user->id }}" {{ $user->id == $order->user_id ? 'selected' : '' }}>{{ $user->name }}</option>
@endforeach
</select>

Setting selected option in laravel from database

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!

Laravel/Blade: pre-selected dropdown option

On your Edit page, you are passing the existing mail message as $MailMessage. I am assuming your database has its value as id, so it can be accessed as $MailMessage->id. While you are looping through and displaying each option, you need to check if that option's value equals the id of the message you are editing. If it matches, you set the selected attribute on the <option>:

<select name="type" id="type" class="form-control">
@foreach ($MailMessageTypes as $value => $label)
<option value="{{$value}}" {{($MailMessage->id == $value) ? 'selected' : ''}}>
{{$label}}
</option>
@endforeach
</select>

In your HTML your <select> would end up looking something like:

<select name="type" id="type" class="form-control">
<option value="1">COM01</option>
<option value="2" selected>COM02</option>
<option value="3">COM03</option>
// ... the rest of the options
</select>

Laravel select2 set selected option on blade file

there are a lot of ways to do this
let's start with the simplest one
I will suppose we have companies' array

$companies = ['google', 'facebook', 'amazon'];
$selectedCompany = 1;

1- you can just select the equivalent key

<option value="{{$key}}" {{ ($key === $selectedCompany) ? 'selected' : '' }}>{{$value}}</option>

2- you can use Components to build Option Component
https://laravel.com/docs/8.x/blade#components

from CLI run

php artisan make:component Forms/Select/Option

laravel will create two files one for the view code and the second for the logic

resources/views/components/forms/select/option.blade.php
app/View/Components/Forms/Select/Option.php

create three properties $key, $value and $selectedKey

<?php

namespace App\View\Components\Forms\Select;

use Illuminate\View\Component;

class Option extends Component
{
public $value;

public $key;

public $selectedKey;
/**
* Create a new component instance.
*
* @return void
*/
public function __construct($key, $value, $selectedKey)
{
$this->key = $key;
$this->value = $value;
$this->selectedKey = $selectedKey;
}

/**
* Get the view / contents that represent the component.
*
* @return \Illuminate\Contracts\View\View|\Closure|string
*/
public function render()
{
return view('components.forms.select.option');
}
}

and the option.blade.php content

<option value="{{$key}}" {{ $key === $selectedKey ? 'selected' : '' }}>{{$value}}</option>

now you can call this component wherever you want and pass it the properties

<x-forms.select.option :value="$value" :key="$key" :selectedKey="$selectedCompany"> </x-forms.select.option>

3- you can use laravelcollective/html package
https://laravelcollective.com/docs/6.x/html#drop-down-lists

form the CLI run

composer require laravelcollective/html

now you can use the Form::select in any blade view file

{{ Form::select('company_id', $companies, $selectedCompany) }}

I hope it's helpful

Define the selected option with the old input in Laravel / Blade

The solution is to compare Input::old() with the $keyvariable using Blade Directives - If Statements.

@if (Input::old('title') == $key)
<option value="{{ $key }}" selected>{{ $val }}</option>
@else
<option value="{{ $key }}">{{ $val }}</option>
@endif

Depending on Select option change others value on div and input field in Laravel

I have figured out a solution for this.
Here is my code:

 <select name="offer_id" id="parent_id" class="form-control dynamic" data-dependent="details">
<option value=""> Select Offer</option>
@foreach ( $offers as $row )
<option value="{{ $row->id }}">{{ $row->name }}</option>
@endforeach
</select>
@foreach($offers as $row)
<div class="some" id="some_{{ $row->id }}" style="display:none;">
{{ $row->details }}
</div>
@endforeach

<script type="text/javascript">
$('#parent_id').on('change',function(){
$(".some").hide();
var some = $(this).find('option:selected').val();
$("#some_" + some).show();}); </script>



Related Topics



Leave a reply



Submit