Laravel & Buefy - Get the old selected value - laravel

I'm using Laravel 5.8 and Buefy... I'm trying to keep the old value for the <b-select>, but my code didn't work.
<b-field>
<b-select name="nationality_id"
placeholder="{{__('site.nationality')}}"
icon="flag"
icon-pack="fas"
expanded>
#foreach ($nationalities as $nationality)
<option value="{{ $nationality->id }}" {{(old('nationality_id')==$nationality->id)? 'selected':''}}>
{{ $nationality->name}}
</option>
#endforeach
</b-select>
</b-field>
I know that I need v-model but I need help with using it.

You should use value prop, for example value="{{old('nationality_id')}}"

Related

Livewire uncaught (in promise) DOMException with select

I have a dropdown in my component that seems to be causing an error in Livewire.
Uncaught (in promise) DOMException: Failed to execute 'setAttribute'
on 'Element': '?' is not a valid attribute name.
I added the wire:key code based on what I read in the docs under the troubleshooting section, but it didn't seem to help. It updates the value on save just like it should, but when it refreshes the dom with the new data, it's not working.
Here's the offending element:
<div class="mb-3 col-md-4 ">
<label for="state" class="form-label">State</label>
<select wire:model.defer="location.state" class="form-select"
id="state" name="state">
#foreach ($states as $state)
<option
name="{{ $state->name }}"
id="{{ $state->code }}"
wire:key="{{ $state->id }}"
value="{{ $state->code }}"
#if (isset($location->state)
&& $location->state === $state->code) ? selected #endif
>{{ $state->name }}
</option>
#endforeach
</select>
</div>
You have a typo here,
#if (isset($location->state) && $location->state === $state->code)
? selected
#endif
Where it tries to apply ? as an attribute on the element, but ? is not a valid attribute. Just remove the ?.
That said, you cannot use the selected property on <select> elements in Livewire to set the selected value. You need to set the value of your wire:model in your component. This means that you have to remove the #if(..) selected #endif from your Blade entirely, and instead set the value in your component,
public function mount() {
$this->location['state'] = $state->code;
}

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

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.

Laravel and selectize add option

I use laravel 5.6 and selectize, everything work great but I won't be able to add option or to set value with jQuery.
Here my code :
var select_user, $select_user;
$select_user = $('#select-user').selectize();
select_user = $select_user[0].selectize;
select_user.addOption({id: 10, name: 'name'});
And my html :
<select id="select-user" name="user" class="form-control" placeholder="Sélectionnez un utilisateur" required>
<option value="">Sans</option>
#foreach($users as $user)
<option value="{{$user->id}}">{{$user->name }}</option>
#endforeach
</select>
I have my users from my bdd but not my added one. No error message too.
Someone can tell me where I'm wrong ?
Thank for your help.

How to show selected value from database in dropdown using Laravel?

I want to show selected value from database into dropdown list on page load.
My controller index function is :
public function index()
{
$country_data =DB::table('country')->select('country_id','country_name')->get();
$profile_data= DB::table('profiles')->select('*')->where('id',$user_id)->first();
return view('profile_update',compact('profile_data','country_data'));
}
Column name in database for height is :Height
My dropdown in profile_update.blade.php is
<select class="select4" name="country" id="country">
<option value="">Please Select</option>
#foreach($country_data as $country)
<option value="{{$country->country_id}}" {{$country_data->country == $country->country_id ? 'selected' : ''}}>{{$country->country_name}}</option>
#endforeach</select>
This is a example of how I do this:
<select class="js-states browser-default select2" name="shopping_id" required id="shopping_id">
<option value="option_select" disabled selected>Shoppings</option>
#foreach($shoppings as $shopping)
<option value="{{ $shopping->id }}" {{$company->shopping_id == $shopping->id ? 'selected' : ''}}>{{ $shopping->fantasyname}}</option>
#endforeach
</select>
In order to understand it fully you will need basics of laravel (MVC),
Let suppose, you have controller. In my case I made a separate table for drop down values.
My Approach --- Separate table for values of drop down, you can try different approach but my explanation was mainly focused on concept.
Note: PersonInfo is model, sampleType is model of my drop down values and don't forget to make a route for the controller.
ExampleController{
public funtion getValues($id){
/*
I am fetching information of a single row from the database by id and
then passing it to the view. The $id to the function came from the
request by your view/form.
I also fetched the values of drop down from the separate table
of database and passed it to the exampleView.
*/
$selectedValue=PersonInfo::findOrFail($id);
$sampleType=SampletypePicker::all(); //model for fetching all values of drop down
return view('exampleView',compact('selectedValue','sampleType));
}
}
So, in the above controller I fetched all values and passed it to the ExampleView. Now in your View file you have to work likewise.
exampleView.blade.php add the below code
<?php $options=$selectedValue->sample_type ?> //note take the value from the database which stored for the individual record and match it with the selected one.
<select class="form-control" name="test">
<option>Select Test Type</option>
#foreach ($sampleType as $value)
<option value="{{ $value->sample_type }}" {{ ( $value->sample_type == $options) ? 'selected' : '' }}>
{{ $value->sample_type }}
</option>
#endforeach
</select>
It is one of the best approach when you want to add dynamic values to the drop down. I mean if you want to add more values to the select tag options then this is the best approach.
Another Approach - take the idea
<?php $months = array("Jan", "Feb", "Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"); ?>
<?php $options=$patient_data->month ?>
#if($patient_data->month)
<select id="expiry_month" name="month" class="form-control-sm">
#foreach($months as $month)
<option value="{{$month}}" {{($month==$options)? 'selected':'' }}>{{$month}}</option>
#endforeach
</select>
#endif
<span class="select-icon"><i class="zmdi zmdi-chevron-down"></i></span>
</div>
OUTPUT of ABOVE CODE
#Sarita Sharma show the error(s). Maybe then anyone help you to resolve this problem. Show data in colections in controler - use dd e.g.
dd($country_data);
dd($profile_data);
Do you want to display the country in the view with the appropriate Height value from profile_data?
Ps. How to put the code, please use the formatting code - put the code in `` and use camelCase in value name (maintaining good PSR-1 practice) - it is easier to read code.
Well, simply if anyone still looking for this, here is the simple way to select items for dropdown from database to blade view
$users = Users::pluck('name', 'id');
$selectedID = 2;
return view('users.edit', compact('id', 'users'));
add this in the controller function before rendering the view
and in the blade view, simply use this variable as foreach loop as
<select name="user_id" class="form-control" id="exampleFormControlSelect1">
#foreach ($users as $key => $value)
<option value="{{ $key }}" {{ ($key == $selectedID) ? 'selected' : '' }}>
{{ $value }}
</option>
#endforeach
</select>
if you don't want to select all users from DB, you can use where condition before pluck condition as
$users = Users::where('status', 1)->pluck('name', 'id');
works with Laravel 8 and Laravel6

How do I call a where function in a Laravel view?

Is there a way to use a where statement in a view?
In the second select input, I want to list all the makes of vehicles in the previously selected year.
<select data-placeholder=" Year" class="chzn-select year" style="width:70px;" tabindex="1" id="year">
<option value=""></option>
#foreach($data['vehicle_years'] as $vehicle)
<option value="{{ $vehicle->year }}">{{ $vehicle->year }}</option>
#endforeach
</select>
<select data-placeholder=" Year" class="chzn-select year" style="width:70px;" tabindex="1" id="year">
<option value=""></option>
#foreach($data['vehicle_makes'] as $vehicle)
<option value="{{ $vehicle->make }}">{{ $vehicle->make }}</option>
#endforeach
</select>
So maybe something along the lines of:
{{ $vehicle->make->where(...) }}
Yes, you can use where() anywhere in a blade in this way:
{{ $vehicle->where('make', $make)->get() }}
But, if you come up with using eloquent in a view, it means there is something wrong in code design. Here are some suggestions:
After the first select is selected by the user, you need to reload the page with selected make, then, you can use it in where() statement in the code design you have now. If there will be no reload, you cannot state what to put in where(). If you will reload the page, then, actually you do not need to use where() in blade..
If you are panning to use the where() in a loop in view, it is better to load all "vehicles and makes" in the controller (if number of items is not too big), and use them (you may need some js)
Alternatively, you may use AJAX to get list of "make" after the vehicle is selected, as it is mentioned in comments.
!!You should not have both selects as the same ID!!
Remember that PHP code is being generated on the backend before it is sent to the client, so it's not possible to use blade to supply a dynamic list unless you are using AJAX to call a new list based on the year and replace the old one. This is heavy lifting though. If you don't have large amounts of data, I'd suggest using regular jQuery to show/hide those that don't match the correct year (maybe by the year being a class). Even better would be to use Vue.js and use v-if to check if the option matches the year selected above.
See Laracasts - Learn Vue 2 (Episode 6) for more details on that.
Here is a quick version of the jQuery answer:
<select data-placeholder=" Year" class="chzn-select year" style="width:70px;" tabindex="1" id="year">
<option value=""></option>
#foreach($data['vehicle_years'] as $vehicle)
<option value="{{ $vehicle->year }}">{{ $vehicle->year }}</option>
#endforeach
</select>
<select data-placeholder=" Year" class="chzn-select year" style="width:70px;" tabindex="1" id="make">
<option value=""></option>
#foreach($data['vehicle_makes'] as $vehicle)
<option class="{{ $vehicle->year }}" value="{{ $vehicle->make }}">{{ $vehicle->make }}</option>
#endforeach
</select>
<script>
$(function() {
var year = $('select#year');
year.change(function() {
var makes = $('select#make > option');
makes.show(); // shows all
// hides all that don't have the selected year as class
makes.each(function( index ) {
if ( !$(this).hasClass( year.val() ) ) $(this).hide();
});
});
});
</script>

Resources