How will i able to select the data using laravel5.2 - php

Hi im new to laravel im using 5.2 and im still learning
My problem is how will i able to select the data from which i queried in the database ?
Here's my view code below
<label>Target Country: </label>
<select name="country" id="country" class="form-control" onchange="getCityList(this.value, 'city')">
<option>Select Country :</option>
#foreach($countries as $country)
<option value="{{$country->id}} " ({{$countryId }} == {{$countryId}}) ? 'selected="selected"' : '' >{{$country->name}}</option>
#endforeach
</select>
And my code to get the data on the database
$countryId = \App\Person::find($this->request->session()->get('newPersonId'))->companies()->get(['country_id'])[0]->country_id;
I want the result to be this way:
<option value="{{$country->id}} " selected='selected; >{{$country->name}}</option>
any help is muchly appreciated! TIA

Try this:
<option value="{{ $country->id }}" {{ $countryId == $country->id ? 'selected="selected"' : ''}}>{{ $country->name }}</option>

Related

How to do show selected option in the phalcon volt syntax

I have an html code something like
<label>Is Pan Verification Enabled</label>
<select class="form-control" name="ispanverified">
<option >Select</option>
<option selected={{ data['ispanverified'] == 'true' ? 'selected' : '' }} value="true">True</option>
<option selected={{ data['ispanverified'] == 'false' ? 'selected' : 'false' }} value="false">False</option>
</select> <br/>
What I want to do is to show the default selected option in the select tag ,but its not working ,can anyone point me out to the right syntax?

How to keep values in <select> after submitting a form in laravel 8

I have multiple <select> by city filter
select in blade
<select id="city" multiple name="city[]">
#foreach($cities as $city)
<option value="{{ $city->id }}">{{ $city->name }}</option>
#endforeach
</select>
Query for select
if ($request->has('industry')) {
$businessesQuery->whereIn('industry_id', $request->input('industry'));
}
try this
#if(request('type') != "")
$('#city').find('option[value="' + "{{request('type')}}" + '"]').attr('selected', true);
#endif
little manage according to your code then its should work

I am working on a Laravel 8 project in which I want to keep the old value in select option in the blade file

I know this question is asked many time but don't know why solution is not working for me. I am a beginner and have a very simple confusion. I have a form in Laravel blade with a drop down. Now I want to keep the drop down value selected after the user submit the form. This is the code I applied to make it work from internet but don't know why it is not working.
<select class="form-control" name="company">
<option value="select">Select Type</option>
<option value="Apple" {{ old('company') == "Apple" ? 'selected' : '' }}>Apple</option>
<option value="Google" {{ old('company') =="Google" ? 'selected' : '' }}>Google</option>
<option value="Mi" {{ old('company') == "Mi"? 'selected' : '' }}>Mi</option>
<option value="Samsung" {{ old('company') == "Samsung" ? 'selected' : '' }}>Samsung</option>
</select>

Getting selected option on edit form in laravel 6

i want to edit user profile which has a country, country field is a select option, i want to get a user's country value in select field, i tried the answer in stack but it didn't work for me knowing there is relationship between user and country belongsTo hasMany.
edit.blade.php
<select id="country" name="country_id" class="form-control" >
<option value="" selected disabled>Select</option>
#foreach($countries as $country)
<option value="{{$country->id}} {{ $country->id == $users->country_id ? 'selected' : '' }}"> {{ $country->name }}</option>
#endforeach
</select>
usersController.php
public function edit($id)
{
$users = Auth::user();
$countries = Country::all();
return view('users.edit')->with([
'users' => $users,
'countries' => $countries
]);
}
Instead of this:
<option value="{{$country->id}} {{ $country->id == $users->country_id ? 'selected' : '' }}"> {{ $country->name }}</option>
I believe you want this:
<option value="{{$country->id}}"{{ $country->id == $users->country_id ? ' selected' : '' }}> {{ $country->name }}</option>

duplicate value on select2 tag on laravel blade template

I found a bug when i try to displayed data on select2 tag. I got duplicate data and I want to know how to fixed it. Im using laravel 7 and here's my code
This is my controller code:
public function editLocation(Request $request,$id){
$mtFasilitas = DB::table('MT_Facility')->select('id','name')
->get();
$trFasilitas = DB::table('TR_Fasilitas')->select('idMtFasilitas')
->where('idDetailLokasi',$id)
->get();
return view('layout.back.content_kos.edit_kos',['mtFas' => $mtFasilitas, 'trFas' => $trFasilitas]);
}
This is my blade template :
<select class="js-example-basic-multiple form-control mb-4" name="fasilitas[]" multiple="multiple">
#foreach ($mtFas as $key => $data)
#foreach ($trFas as $key2 => $data2)
<option value="{{$data->id}}"{{$data2->idMtFasilitas == $data->id ? 'selected="selected"' : ''}}> {{ $data->name}}</option>
#endforeach
#endforeach
</select>
And this is the result :
enter image description here
Anyone can help me ?
Thank you
$trFasilitas = DB::table('TR_Fasilitas')->select('idMtFasilitas')
->where('idDetailLokasi',$id)
->get()
->pluck('idMtFasilitas')
->all();
<select class="js-example-basic-multiple" name="fasilitas[]" multiple="multiple">
#foreach ($mtFas as $data)
<option value="{{ $data->id }}" {{in_array($data->id, $trFas) ? 'selected="selected"' : '' : ''}}>
{{ $data->name }}</option>
#endforeach
</select>
I think this should do.
This is the result of your updated answer sir #Andy Song
As per your data
$mtFasilitas = [{"id":4,"name":"a"},{"id":5,"name":"b"},{"id":6,"name":"c"}]
$trFasilitas = [{"idMtFasilitas":4},{"idMtFasilitas":5}]
Your inner loop running twice for each single value of outer loop, thats why data printed twice.

Categories