Select OnChange Javascript on Laravel - javascript

I want to make it so that when I click on one of the dropdowns, the value according to the record id in the related table immediately appears.
I want to make it so that when I click on one of the dropdowns, the value according to the record id in the related table immediately appears.
when "golongan" is selected then the value of "gaji pokok" automatically appears.
when "asisten ahli" is selected then the value of "tunjangan fungsional" automatically appears.
when "pembantu ketua | asisten ahli" is selected then the value of "tunjangan struktural" automatically appears.
<div class="modal fade" id="tambahgajiModal" tabindex="-1" role="dialog" aria-labelledby="tambahgajiModal" aria-hidden="true" data-backdrop="static" data-keyboard="false">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="tambahfungsiModal">Tambah Gaji Karyawan</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form action="/gaji/insert" method="POST" enctype="multipart/form-data">
#csrf
<div class="content">
<div class="row">
<div class="col-sm-12">
<div class="form-group">
<select name="nama" type="text" class="form-control select2 #error('nama') is-invalid #enderror" value="{{ old('nama') }}">
<option>-- Nama Karyawan --</option>
#foreach ($karyawan as $data)
<option value="{{ $data->nama }}">{{ $data->nama }}</option>
#endforeach
</select>
<div class="invalid-feedback">
#error('nama')
{{ $message }}
#enderror
</div>
</div>
<div class="form-group">
<select name="gol" type="text" class="form-control select2 #error('gol') is-invalid #enderror" onchange="getddl()" value="{{ old('gol') }}">
<option>-- Golongan dan M K G --</option>
#foreach ($golongan as $data)
<option value="{{ $data->gol }} | {{ $data->mkg }}">{{ $data->gol }} | {{ $data->mkg }}</option>
#endforeach
</select>
<div class="invalid-feedback">
#error('gol')
{{ $message }}
#enderror
</div>
</div>
<div class="form-group">
<input name="tunjangan_gol" type="text" placeholder="Gaji Pokok" class="form-control #error('tunjangan_gol') is-invalid #enderror" value="{{ old('tunjangan_gol') }}" id="gol">
<div class="invalid-feedback">
#error('tunjangan_gol')
{{ $message }}
#enderror
</div>
</div>
<div class="form-group">
<select name="jbt_fungsi" type="text" class="form-control select2 #error('jbt_fungsi') is-invalid #enderror" value="{{ old('jbt_fungsi') }}">
<option>-- Jabatan Fungsional --</option>
#foreach ($fungsi as $data)
<option value="{{ $data->jbt_fungsi }}">{{ $data->jbt_fungsi }}</option>
#endforeach
</select>
<div class="invalid-feedback">
#error('gol')
{{ $message }}
#enderror
</div>
</div>
<div class="form-group">
<input name="tunjangan_fungsi" type="text" placeholder="Tunjangan Fungsional" class="form-control #error('tunjangan_fungsi') is-invalid #enderror" value="{{ old('tunjangan_fungsi') }}">
<div class="invalid-feedback">
#error('tunjangan_fungsi')
{{ $message }}
#enderror
</div>
</div>
<div class="form-group">
<select name="jbt_struktur" type="text" class="form-control select2 #error('jbt_struktur') is-invalid #enderror" value="{{ old('jbt_struktur') }}">
<option>-- Jabatan Struktural & Fungsional --</option>
#foreach ($struktur as $data)
<option value="{{ $data->jbt_struktur }} | {{ $data->jbt_fungsi }}">{{ $data->jbt_struktur }} | {{ $data->jbt_fungsi }}</option>
#endforeach
</select>
<div class="invalid-feedback">
#error('jbt_struktur')
{{ $message }}
#enderror
</div>
</div>
<div class="form-group">
<input name="tunjangan_struktur" type="text" placeholder="Tunjangan Struktural" class="form-control #error('tunjangan_struktur') is-invalid #enderror" value="{{ old('tunjangan_struktur') }}">
<div class="invalid-feedback">
#error('tunjangan_struktur')
{{ $message }}
#enderror
</div>
</div>
<div class="form-group">
<input name="total_gaji" type="text" placeholder="Total Gaji" class="form-control #error('total_gaji') is-invalid #enderror" value="{{ old('total_gaji') }}">
<div class="invalid-feedback">
#error('total_gaji')
{{ $message }}
#enderror
</div>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Save</button>
</div>
</form>
</div>
</div>
</div>
how to package it with Javascript onChange in laravel ?

Sorry, I couldn't understand your language well, but you can do something like this with jquery
<div class="form-group mb-3">
<select id="country-dropdown" class="form-control">
<option value="">-- Select Country --</option>
#foreach ($countries as $data)
<option value="{{$data->id}}">
{{$data->name}}
</option>
#endforeach
</select>
</div>
<div class="form-group mb-3">
<select id="state-dropdown" class="form-control">
</select>
</div>
<div class="form-group">
<select id="city-dropdown" class="form-control">
</select>
</div>
you can leave the dependent dropdown empty, then get those depentdent dropdown through ajax like these:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$(document).ready(function () {
/*------------------------------------------
--------------------------------------------
Country Dropdown Change Event
--------------------------------------------
--------------------------------------------*/
$('#country-dropdown').on('change', function () {
var idCountry = this.value;
$("#state-dropdown").html('');
$.ajax({
url: "{{url('api/fetch-states')}}",
type: "POST",
data: {
country_id: idCountry,
_token: '{{csrf_token()}}'
},
dataType: 'json',
success: function (result) {
$('#state-dropdown').html('<option value="">-- Select State --</option>');
$.each(result.states, function (key, value) {
$("#state-dropdown").append('<option value="' + value
.id + '">' + value.name + '</option>');
});
$('#city-dropdown').html('<option value="">-- Select City --</option>');
}
});
});
/*------------------------------------------
--------------------------------------------
State Dropdown Change Event
--------------------------------------------
--------------------------------------------*/
$('#state-dropdown').on('change', function () {
var idState = this.value;
$("#city-dropdown").html('');
$.ajax({
url: "{{url('api/fetch-cities')}}",
type: "POST",
data: {
state_id: idState,
_token: '{{csrf_token()}}'
},
dataType: 'json',
success: function (res) {
$('#city-dropdown').html('<option value="">-- Select City --</option>');
$.each(res.cities, function (key, value) {
$("#city-dropdown").append('<option value="' + value
.id + '">' + value.name + '</option>');
});
}
});
});
});
</script>
and declare the routes that you will call in url in ajax
Route::post('etch-states', [DropdownController::class,
'fetchState']);
Route::post('fetch-cities', [DropdownController::class, 'fetchCity']);
In the controller do something like this:
public function fetchState(Request $request)
{
$data['states'] = State::where("country_id", $request->country_id)
->get(["name", "id"]);
return response()->json($data);
}
/**
* Write code on Method
*
* #return response()
*/
public function fetchCity(Request $request)
{
$data['cities'] = City::where("state_id", $request->state_id)
->get(["name", "id"]);
return response()->json($data);
}

Related

How to convert string to integer then sum in javascript

I have two text boxes that will have currency strings in them which I then need to sum from the two text boxes into the total text box
srv_trk
but, the result an error like this:
how to fix this?
total_price
how to delete rupiah so that srvPrice and trkPrice return to integer and the total is not error?
the code:
<script>
const rupiah = (number)=>{
return new Intl.NumberFormat("id-ID", {
style: "currency",
currency: "IDR"
}).format(number);
}
</script>
<script type="text/javascript">
$("#service").change(function(){
var element = $("option:selected", this);
var srvPrice = element.attr("srvPrice");
var trkPrice = $('#trkPrice').val();
var total = Number(trkPrice) + Number(srvPrice);
$('#srvPrice').val(rupiah(srvPrice));
$('#totalPrice').val(rupiah(total));
});
</script>
<script type="text/javascript">
$("#trucking").change(function(){
var element = $("option:selected", this);
var trkPrice = element.attr("trkPrice");
var srvPrice = $('#srvPrice').val();
var total = Number(trkPrice) + Number(srvPrice);
$('#trkPrice').val(rupiah(trkPrice));
$('#totalPrice').val(rupiah(total));
});
</script>
<div class="form-group row">
<label class="col-sm-4 col-form-label" for="service">Service Name</label>
<div class="col-sm-8">
<select class="form-control selectric" name="service_id" id="service">
<option srvPrice="" selected>Select an Option</option>
#foreach ($service as $s)
#if(old('service_id') == $s->service_id)
<option value="{{ $s->service_id }}" {{ old('service_id') == $s->service_id ? 'selected' : '' }} srvPrice="{{ $s->service_price }}">{{ $s->service_name }}</option>
#else
<option value="{{ $s->service_id }}" srvPrice="{{ $s->service_price }}">{{ $s->service_name }}</option>
#endif
#endforeach
</select>
</div>
</div>
<div class="form-group row">
<label class="col-sm-4 col-form-label" for="trucking">Trucking</label>
<div class="col-sm-8">
<select class="form-control selectric" name="trucking_id" id="trucking">
<option trkPrice="" selected>Select an Option</option>
#foreach ($trucking as $t)
#if(old('trucking_id') == $t->trucking_id)
<option value="{{ $t->trucking_id }}" {{ old('trucking_id') == $t->trucking_id ? 'selected' : '' }} trkPrice="{{ $t->trucking_price }}">{{ $t->trucking_name }}</option>
#else
<option value="{{ $t->trucking_id }}" trkPrice="{{ $t->trucking_price }}">{{ $t->trucking_name }}</option>
#endif
#endforeach
</select>
</div>
</div>
<div class="form-group row">
<label class="col-sm-4 col-form-label">Service Price</label>
<div class="col-sm-8">
<input type="text" class="form-control" id="srvPrice" required readonly>
<div class="invalid-feedback">
Please make a selection on Service Option.
</div>
</div>
</div>
<div class="form-group row">
<label class="col-sm-4 col-form-label">Trucking Price</label>
<div class="col-sm-8">
<input type="text" class="form-control" id="trkPrice" readonly>
</div>
</div>
<div class="form-group row">
<label class="col-sm-4 col-form-label">Total Price</label>
<div class="col-sm-8">
<input type="text" class="form-control" id="totalPrice" required readonly>
<div class="invalid-feedback">
Please make a selection on Service Option.
</div>
</div>
</div>
You can simply achieve this with a single line of code by using String.replace() method along with parseInt()
Live Demo :
const servicePrice = 'Rp 550.000,00';
const truckingPrice = 'Rp 100.000,00';
const totalPrice = parseInt(servicePrice.replace('Rp', '').trim()) + parseInt(truckingPrice.replace('Rp', '').trim());
console.log(totalPrice);
Maybe you can just use the replace function on both strings before adding them up. (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace)
So just variableName.replace("Rp ", "")
If the Number is written in text format e.g. number = "5", then you can use parseInt(number) to convert it to integer, then you will be able to treat it as a normal number.
Example:
let text = "5", x = 10;
console.log(parseInt(text) + x);

Laravel 8 how to show & hide or grayed out Div when another Div Dropdown selection value of foreignkey from different table

I want to show & hide or grayed out Text forms based on the Another Div dropdown selection and this dropdown selection is foreignkey element from different table.
attendance_type
table will have AB \ PR values respectively
This is my dropdown blade code
<div class="form-group">
<label for="attendance_type_id">{{ trans('cruds.attendance.fields.attendance_type') }}</label>
<select class="form-control select2 {{ $errors->has('attendance_type') ? 'is-invalid' : '' }}" name="attendance_type_id" id="attendance_type_id">
#foreach($attendance_types as $id => $entry)
<option value="{{ $id }}" {{ old('attendance_type_id') == $id ? 'selected' : '' }}>{{ $entry }}</option>
#endforeach
</select>
#if($errors->has('attendance_type'))
<div class="invalid-feedback">
{{ $errors->first('attendance_type') }}
</div>
#endif
<span class="help-block">{{ trans('cruds.attendance.fields.attendance_type_helper') }}</span>
</div>
When i select the AB value selected then following should be displayed or grayed out from editing and if select PR value from the attendance_type table following div code should be displayed!
<div class="form-group">
<label for="in_time">{{ trans('cruds.attendance.fields.in_time') }}</label>
<input class="form-control timepicker {{ $errors->has('in_time') ? 'is-invalid' : '' }}" type="text" name="in_time" id="in_time" value="{{ old('in_time') }}">
#if($errors->has('in_time'))
<div class="invalid-feedback">
{{ $errors->first('in_time') }}
</div>
#endif
<span class="help-block">{{ trans('cruds.attendance.fields.in_time_helper') }}</span>
</div>
<div class="form-group">
<label for="out_time">{{ trans('cruds.attendance.fields.out_time') }}</label>
<input class="form-control timepicker {{ $errors->has('out_time') ? 'is-invalid' : '' }}" type="text" name="out_time" id="out_time" value="{{ old('out_time') }}">
#if($errors->has('out_time'))
<div class="invalid-feedback">
{{ $errors->first('out_time') }}
</div>
#endif
<span class="help-block">{{ trans('cruds.attendance.fields.out_time_helper') }}</span>
</div>
My Blade Code
<div class="form-group" id="InTime_dis">
<label for="in_time">{{ trans('cruds.attendance.fields.in_time') }}</label>
<input class="form-control timepicker {{ $errors->has('in_time') ? 'is-invalid' : '' }}" type="text" name="in_time" id="in_time" value="{{ old('in_time') }}">
#if($errors->has('in_time'))
<div class="invalid-feedback">
{{ $errors->first('in_time') }}
</div>
#endif
<span class="help-block">{{ trans('cruds.attendance.fields.in_time_helper') }}</span>
</div>
<div class="form-group" id="OutTime_dis">
<label for="out_time">{{ trans('cruds.attendance.fields.out_time') }}</label>
<input class="form-control timepicker {{ $errors->has('out_time') ? 'is-invalid' : '' }}" type="text" name="out_time" id="out_time" value="{{ old('out_time') }}">
#if($errors->has('out_time'))
<div class="invalid-feedback">
{{ $errors->first('out_time') }}
</div>
#endif
<span class="help-block">{{ trans('cruds.attendance.fields.out_time_helper') }}</span>
</div>
My JsScript Code
<script>
$(document).ready(function(){
$('#attendance_type_id').on('change', function() {
if ( this.value == '2')
{
$("#InTime_dis").show();
$("#OutTime_dis").show();
}
else
{
$("#InTime_dis").hide();
$("#OutTime_dis").hide();
}
});
});
</script>
Hope this might help laravel beginners!

How to preserve all the filters in the form after reloading the view in Laravel 5.4?

I have the following form in the view:
<form class="form-inline" method="POST" action="{{route('dsp.report')}}">
#csrf <!-- {{ csrf_field() }} -->
<div class="col-sm-2 form-group">
<label>Range one: </label><input type="text" class="form-control" id="date_from" name="date_from" placeholder="Date from range">
</div>
<div class="col-sm-2 form-group">
<label>Range two: </label><input type="text" class="form-control" id="date_to" name="date_to" placeholder="Date to range">
</div>
<div class="col-sm-2 form-group">
<label>Filter by SSP: </label><select name="ssp_id[]" id="ssp" class="form-control-lg select2" multiple="multiple"></select>
</div>
<div class="col-sm-2 form-group">
<label>Filter by DSP: </label><select name="dsp_id[]" id="dsp" class="form-control-lg select2" multiple="multiple"></select>
</div>
<div class="col-sm-2 form-group">
<label>Group by: </label>
<select class="form-control" name="groupby">
<option>SSP/DSP</option>
<option>SSP</option>
<option>DSP</option>
</select>
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
I want to preserve all the values in this form after controller processed the request and returned data back:
return view('dsp.index', $data);
How can I do it? Is there any simple solution?
UPD: To make it clear for preserving values (not keys) in select2-based multiple dropdown ajax selects:
First of all, we trigger this controller when we're looking for the SSP term:
public function get_ssp(Request $request) {
$term = $request->get('term');
$ssp_list = SspCompanyList::where('ssp_company_name', 'LIKE', '%'. $term['term'] .'%')
->get(['ssp_company_id as id', 'ssp_company_name as text']);
return ['results' => $ssp_list];
}
And then we process chosen ids in the controller after submitting. Select2 has been configured in this way:
$(document).ready(function() {
$('#ssp').select2({
width: '200px',
minimumInputLength: 3,
ajax: {
url: '{{ route("get_ssp") }}',
dataType: 'json',
type: "POST",
quietMillis: 50,
data: function (term) {
return {
term: term
};
},
},
});
});
but the problem is that select2 sends only ids in ssp_id[], not key-value items, and it's not possible to recover them from old() function as item names before sending.
UPD2:
I'm trying to solve it in this way:
<select name="ssp_id[]" id="ssp" class="form-control-lg select2" multiple="multiple">
#if (is_array(old('ssp_id')))
#foreach (old('ssp_id') as $ssp_id)
{{ $ssp_name = \App\Models\SspCompanyList::select('ssp_company_name')->where('ssp_company_id', $ssp_id)->first()->ssp_company_name }}
<option value="{{ $ssp_name }}" selected="selected">{{ $ssp_name }}</option>
#endforeach
#endif
</select>
and after first reload it works and preserves the ssp names I need, but after second attempt to select values for dsp below on the same page and submit again it returns this error:
Trying to get property 'ssp_company_name' of non-object (View:
/home/pavel/projects/dsp/resources/views/dsp/index.blade.php)
So, why first time it's working and second time - not? Should we treat it in the other manner for select2?
NOTE:
You forgot to add the value attribute on your select options
For the normal Input Fields, you can pass the old value to it by using Laravel's old('input_name')
For Example:
<input name="input_name" value="{{ old('input_name') }}" />
While your Select Fields need the selected attribute on each option tag.
For example:
<select class="form-control" name="input_name">
<option {{ old('input_name') == "SSP/DSP" ? 'selected' : '' }} value="SSP/DSP">SSP/DSP</option>
<option {{ old('input_name') == "SSP" ? 'selected' : '' }} value="SSP">SSP</option>
<option {{ old('input_name') == "DSP" ? 'selected' : '' }} value="DSP">DSP</option>
</select>
Your code becomes:
<form class="form-inline" method="POST" action="{{route('dsp.report')}}">
#csrf <!-- {{ csrf_field() }} -->
<div class="col-sm-2 form-group">
<label>Range one: </label>
<input type="text" class="form-control" id="date_from" name="date_from" value="{{ old('date_from') }}" placeholder="Date from range">
</div>
<div class="col-sm-2 form-group">
<label>Range two: </label>
<input type="text" class="form-control" id="date_to" name="date_to" value="{{ old('date_to') }}" placeholder="Date to range">
</div>
<div class="col-sm-2 form-group">
<label>Filter by SSP: </label>
<select name="ssp_id[]" id="ssp" class="form-control-lg select2" multiple="multiple"><option></option></select>
</div>
<div class="col-sm-2 form-group">
<label>Filter by DSP: </label>
<select name="dsp_id[]" id="dsp" class="form-control-lg select2" multiple="multiple"><option></option></select>
</div>
<div class="col-sm-2 form-group">
<label>Group by: </label>
<select class="form-control" name="groupby">
<option {{ old('groupby') == "SSP/DSP" ? 'selected' : '' }} value="SSP/DSP">SSP/DSP</option>
<option {{ old('groupby') == "SSP" ? 'selected' : '' }} value="SSP">SSP</option>
<option {{ old('groupby') == "DSP" ? 'selected' : '' }} value="DSP">DSP</option>
</select>
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
You can add your select2 selected values this way:
let arrayOfSelectedValues = [] // You can keep your selected values here if you know them already
$('#ssp').select2({
width: '200px',
minimumInputLength: 3,
ajax: {
url: '{{ route("ssp") }}',
dataType: 'json',
type: "POST",
quietMillis: 50,
data: function (term) {
return {
term: term
};
},
},
}).val(arrayOfSelectedValues).trigger("change"); // Then add the selected values to the select2() initialisation
Add the property value to all inputs like this:
<input type="text" name="username" value="{{ old('username') }}">
Check the docs for better understanding https://laravel.com/docs/5.4/requests#old-input

Append option to multi select using ajax with keeping the selected value

I am using this multiSelect plugin.
Blade template
<form method="POST" id="report-form" action="{{ route('admin.reports.store') }}" aria-label="{{ __('Create Report') }}">
{{ csrf_field() }}
<div class="row">
<div class="form-group col-md-6">
<label class="control-label mb-10 text-left" for="tableName">Choose Table</label>
<select class="form-control" name="tableName" id="tableName">
<option>Choose</option>
#foreach ( $tables as $table )
<option value="{{ $table }}">{{ slugFormat($table) }}</option>
#endforeach
</select>
#if ($errors->has('tableName'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('tableName') }}</strong>
</span>
#endif
</div>
<div class="col-md-6">
<label class="control-label mb-10 text-left" for="columns">Select Column</label>
<select class="form-control" name="columns[]" id="columns">
<option>Choose</option>
</select>
#if ($errors->has('column'))
<span class="invalid-feedback" role="alert">
<strong>{{ $errors->first('column') }}</strong>
</span>
#endif
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group mb-0 pull-right">
<button name="submit" type="submit" class="btn btn-primary">submit</button>
</div>
</div>
</div>
</form>
jQuery code
$('#report-form').on('change', '#tableName', function(event){
event.preventDefault();
$('#columns').empty();
$('#columns').append('<option value="">Choose</option>');
var table = this.value;
var url = APP_URL + '/admin/reports/columns';
$.ajax({
url: url,
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
type: 'POST',
data: { table: table },
success: function(response) {
if ( response ) {
var columns = response.columns;
var options = '';
for (var i=0; i < columns.length; i++){
options += '<option value="'+ table + '.' + columns[i] + '">' + columns[i] + '</option>';
}
$('#columns').append(options);
$('#columns').multiSelect('refresh', {
selectableOptgroup: true
});
}
},
dataType: 'json'
});
});
When I am changing the table select, I am getting columns belongs to that table in #columns select box, now if I am selecting some columns from #columns seelect box and changing the table, it is clearing the selected value also, I need to check multiple columns from multiple tables.
Github Code showing basic implementation of plugin.

Keep select option value selected during edit in laravel

I am trying to keep select option value selected during the edit. There I used javascript for the dynamic dependent dropdown list. But now I am facing a problem to keep option value selected during the edit. Would someone help me please to solve this problem? edit.blade.php -
<form class="form-material form-horizontal" action="{{ route('admin.commercial.orders.update', $order->id) }}" method="POST">
#method('PUT')
#csrf
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label class="col-md-12" for="name">Buyer Name: </label>
<div class="col-md-12">
<select class="form-control select2" name="name" id="buyerName">
<option value="">Select Buyer</option>
</select>
#if ($errors->has('name'))
<small class="error">{{ $errors->first('name') }}</small>
#endif
</div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label class="col-md-12" for="job_name">Job Name/Title: </label>
<div class="col-md-12">
<select class="form-control select2" name="job_name" id="jobName">
<option value="">Select Job</option>
</select>
#if ($errors->has('job_name'))
<small class="error">{{ $errors->first('job_name') }}</small>
#endif
</div>
</div>
</div>
</div>
</form>
And javascript for dependant dropdown-
<script type="text/javascript">
$(function () {
$(".select2").select2();
});
/** Dependent Dropdowns**/
$(function() {
dynamicDropdown('/admin/commercial/get-buyers', '#buyerName');
$('#buyerName').change(function() {
let url = `/admin/commercial/get-buyer-jobs/${this.value}`;
let target = '#jobName';
dynamicDropdown(url, target);
});
});
function dynamicDropdown(url, selector) {
$.get(url, function (data) {
let $select = $(selector);
$select.find('option').not(':first').remove();
let options = [];
$.each(data, function(index, item) {
options.push(`<option value="${item.id}">${item.name}</option>`);
})
$select.append(options);
});
}
</script>
My edit function is -
public function edit($id)
{
$order = BuyerOrder::find($id);
return view('admin.marchendaising.commercials.orders.edit', compact('order'));
}
Add this to the bottom of your javascript;
$("#buyerName").val("{{old('name')}}");
$("#jobName").val("{{old('job_name')}}");`
You may try this idea:
<select name="buyer">
#foreach(['buyer1', 'buyer2', 'buyer3'] as $buyer)
<option vlaue="{{ $buyer }}"{{ (old('buyer') == $buyer) ? ' selected' : '' }}>{{ $buyer }}</option>
#endforeach
</select>
OR
<select name="buyer">
#foreach(['buyer1', 'buyer2', 'buyer3'] as $buyer)
<option vlaue="{{ $buyer }}"{{ ($order->buyer == $buyer) ? ' selected' : '' }}>{{ $buyer }}</option>
#endforeach
</select>

Categories