Laravel old for selectbox options - laravel

So i'm working on a standalone laravel project (still a bit new to this..). Using the same form blade file adding and editing records. I've set old to the fields for validation checks, however i can't seem to get old working correctly for dropdowns. The select options are loaded in a foreach, and ive got a check to set it to selected if the record on edit matches the dropdown option selected, however when i add the old method to it, select a new option (E.G. "broker") and force validation to throw an error it works as expected & saves whatever i've selected (option "broker"), it saves the option on submit, but then when i go to edit another record (option "assessor") it displays the same option i previously selected (option "broker")...if any of that made sense!
Here's the foreach with option select from the blade file:
<select class="form-control" name="other_party_details[party_type_id]" id="other_party_details[party_type_id]">
<option value="">--- Please Select ---</option>
#foreach($partyTypes as $partyType)
<option value="{{ $partyType->id }}" {{old('other_party_details.party_type_id', !empty($claim->otherParty->party_type_id)) == $partyType->id ? 'selected' : '' }}>
{{ $partyType->label }}
</option>
#endforeach
</select>
What am i doing wrong?
I've exhausted google as for the most it seems to show results for multi select (mine is not), or using LaravelCollective (which i'm not), so running out of places to look!
Thanks in advance

You can try below code
<option value="{{$partyType->id}}" #if(old('party_type_id') == $partyType->id || $claim->otherParty->party_type_id == $partyType->id ) selected #endif> {{ $partyType->label }}</option>

Related

Problem passing data from one component to another in livewire

Good day, I am passing information from one component to another from a SELECT, when I do not load the information from the json file it works, but when I extract from the json I have the following error.
ErrorException
Trying to get property 'numero_prestamo_original' of non-object (View: C:\laragon\www\prestamos-cth\resources\views\livewire\prestamos\prestamos.blade.php)
The select field:
<option value="2022006799">2022006799</option>
#foreach ($prestamos as $prestamo => $valor)
<option value="{{ $valor->numero_prestamo_original }}">{{ $valor->numero_prestamo_original }}
</option>
#endforeach
Do you have any idea what is happening. thank you very much.
I have seen on the internet to make the change from -> to [ ] but it doesn't work either.
Thank you all for your help, the solution I found for that problem was to do the search with the required fields in the array and only print what is needed. Thanks for your help.

Livewire ignore does not receive correct data

I'm using selectize.js for dropdown styling with livewire. I'm using livewire for a data table with sortable columns and pagination. The issue is, every time I make pagination or a sort by column, the javascript goes missing thus, there's no styling for the dropdown. I've solved the styling issue using wire:ignore. Now the new problem that I have is that the data passed to the dropdown is not accurate.
#foreach($applications as $application)
<p>{{$application->status}}</p>
<div wire:ignore>
<select class="selectize" name="status" data-width="200px">
#foreach(['Pending',
'Hired',
'Under consideration'] as $status)
<option
#if($application->status === $status) selected #endif>{{ $status }}</option>
#endforeach
</select>
</div>
#endforeach
Inside the <p>{{$application->status}}</p> tag, I get the status 'Pending' but on the dropdown, it shows 'Hired'. The correct status is 'Pending'.
(from comment) #stalwart1014 for example, when I use "select2" I do this
in content section
<select id="select2" wire:model="some">
//.....
</select>
in script section
$(document).ready(function() {
window.initSelectDrop=()=>{
$('#select2').select2({
placeholder: '{{ __('Select') }}',
allowClear: true});
}
initSelectDrop();
window.livewire.on('select2',()=>{
initSelectDrop();
});
});
and in component
public function hydrate()
{
$this->emit('select2');
}
This not always function properly with JS element...but hope this can help you. Greetings
it's maybe a bit late, but worth to mention for the future, I will do not point directly to your issue but explain the workaround of wire:ignore, the wire:ignore directive ignores the updates or changes for the further requests and updates, this attributes is defined for playing with JS third party library, the wire:ignore directive prevent all it's nested elements from updating if you wish to except Childs from updating you can use wire:ignore.self directive

Laravel VUE JS dynamic drop down menu

Why doesn't this work?
Here is my select tag:
<select class="form-control" v-model="provider">
<option value="0">Select Provider</option>
<option v-for="provider in providers" :value="provider.provider_id">{{provider.name}}</option>
</select>
Code which loads the data:
loadProviders(){
axios.get('api/provider').then(({data}) => (this.providers = data.data));
data is then stored in:
data(){
return{
providers : {}
}
}
I've checked the developer networks tab of Chrome and it does return the data from the database.
However the value(provider.name) doesnt show up in the dropdown options menu.
This issue has already been solved: the model for Provider had an error all along.
The issue is, that you are destructuring the API response, but don't take into account that when assigning to data.
axios.get('api/provider').then((data) => (this.providers = data.data));
and
axios.get('api/provider').then(({data}) => (this.providers = data));
both work.
Update your select box
<select v-model="provider">
<option v-for="(value,key) in provider"
:value="value.provider_id">
{{value.name}}
</option>
</select>
Make sure you received proper data from your API.
assign data to provider variable. it will appeare in select box

How to show selected checkbox in multiselect checkbox list in laravel?

I have a multi-select checkbox list. I want to show stored value in list using checkbox selected.
User informations are stored in Partner_Prefence table and user religion column named as p_religion
$profile_data= DB::table('partner_prefence')->select('p_religion')->first();
Fetching religions from religions table
$religion_data=DB::table('religion')->select('religion_id','religion_name')->orderby('religion_name')->get();
Multiselect list
<select multiple="multiple" name="religion[]">
#foreach($religion_data as $religion)
<option value="{{$religion->religion_id}}" {{$profile_data->p_religion == $religion->religion_id ? 'selected' : ''}}>{{$religion->religion_name}}</option>
#endforeach
</select>
I'm having trouble with showing which religions user have
{{$profile_data->p_religion == $religion->religion_id ? 'selected' : ''}}
as I understand you have multi select form, so you need show selected multiple column..
You're storing ids as a string but it's hard check that certain number in string. İf you convert string into a array, you can easly check with in_array() method. This method will return true if given value exist in given array
<select multiple="multiple" name="religion[]">
{{-- no need to explode every time, it will reduce your performance --}}
#php($religions = explode(',', $profile_data->p_religion))
#foreach($religion_data as $religion)
<option
value="{{$religion->religion_id}}"
{{--if user religion id exist in religions then mark as selected--}}
{{in_array($religion->religion_id,$religions) ? "selected" : ""}}>
{{$religion->religion_name}}
</option>
#endforeach
</select>
Is the p_religion column saving multiple IDs if it is a multi-select list? Would using in_array() work then instead of using $profile_data->p_religion == $religion->religion_id.
in_array ($religion->religion_id, explode(',', $profile_data->p_religion))
Added the explode() call on the off chance you are storing an imploded array.
You could also try and use the blade syntax for an if statement inline to see if it displays differently.
<select multiple="multiple" name="religion[]">
#foreach($religion_data as $religion)
<option value="{{$religion->religion_id}}" #if($profile_data->p_religion == $religion->religion_id) selected #endif>
{{$religion->religion_name}}
</option>
#endforeach
</select>

Codeigniter dropdown default to last item?

I create an array of numbers for ordering a gallery. I wanted to know if there was a way to have the dropdown default to the last item instead of the first? So with the example code when the page loads it would have the 6th element (value 5) selected as default.
<php? // Codeigniter that generates the select dropdown.
form_dropdown('order', $order);
?>
<select name="order">
<option value="0">1</option>
<option value="1">2</option>
<option value="2">3</option>
<option value="3">4</option>
<option value="4">5</option>
<option value="5">6</option>
</select>
With your update, I am assuming you want to achieve the result below with the CodeIgniter form_dropdown() function?
<option value="5" selected="selected">6</option>
What you would have to do is add a few parameters to the function like this:
echo form_dropdown('order', '', '5');
Try this,
form_dropdown('order', $order, 5);
Check form_dropdown in CI's userguide for this
If you want to fetch the last element dynamically then use end() and key() functions to fetch the key of the last element of the array.
So here goes the code.
form_dropdown('order', $order, key(end($order)));

Resources