Laravel and selectize add option - laravel

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.

Related

how to dynamically option the selected in html just using php?

I'm trying to dynamically the select option selected just using php in laravel but I'm getting this error:
Use of undefined constant selected - assumed 'selected' (this will
throw an Error in a future version of PHP) (View:
C:\xampp\htdocs\laralast\resources\views\view.blade.php)
below is my view blade
<select class="form-control" name="assign_to" id="assign_to">
<option selected disabled>Select support</option>
#foreach($supports as $support)
<option value="{{$support->id}}" {{($ticket->assign_by == $support->id ? selected : '')}}>{{$support->fullname}}</option>
#endforeach
</select>
Can you help me with this.
You should quote your string (I mean you should quote the selected):
<option value="{{$support->id}}" {{($ticket->assign_by == $support->id ? 'selected' : '')}}>{{$support->fullname}}</option>
Your laravel is understanding that you will print the value of selected constant (since no dollar-sign $, no string quotation '' "") when the condition is true.
Please Check This use if & else
<select class="form-control" name="assign_to" id="assign_to">
<option selected disabled>Select support</option>
#foreach($supports as $support)
#if($ticket->assign_by == $support->id)
<option value="{{$support->id}}" selected>{{$support->fullname}}
</option>
#else
<option value="{{$support->id}}">{{$support->fullname}}</option>
#endforeach
</select>

Laravel & Buefy - Get the old selected value

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')}}"

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>

Laravel Collective select to output null when submitted

I am using Laravel Collective for creating my webform.
{!! Form::select('сity_from', ['London', 'Tokyo', 'Moscow'], null, ['placeholder' => 'Choose city'] ) !!}
which produces the following html:
<select id="сity_from" name="сity_from">
<option selected="selected" disabled="disabled" hidden="hidden" value>Choose city</option>
<option value="London">London</option>
<option value="Tokyo">Tokyo</option>
<option value="Moscow">Moscow</option>
when I choose no city and submit form, and then dd($request->all());in Controller
i can see nothing, I mean, there is no $request->all()['city_from'];
I would like to get ['city_from' = null] in this case.
I suppose I have to change 'value' in
<option selected="selected" disabled="disabled" hidden="hidden" value>Choose city</option>
to value="null"?
Or something else?
I would like to be using Laravel Collective when solving this problem.
I suggest you to not bother with the presence of 'city_from' in your request.
You might use $cityForm = $request->input('city_from');
And you will have $cityForm set to the actual value, or to null
public function store(Request $request)
{
$cityForm = $request->input('city_from'); //will always be actual value or null
}
remove
disabled="disabled"
and add
value="null"
hope it'll solve the problem.

How to select a default option using thymeleaf

I have a drop down like following,
<select id="role" class="form-control" th:field="*{role}" >
<option th:field="*{role}" value="USER">USER</option>
<option selected th:field="*{role}" value="ADMIN" >ADMIN</option>
<option th:field="*{role}" value="SUPERUSER">SUPERUSER</option>
</select>
I want default option as ADMIN. For that i added selected inside option tag. but default option still pointing to USER. Can anyone help me o solve this. Thanks
Set the value of "role" to "ADMIN" in your controller (in the java), before the page is rendered.
Also, you don't need all those extra th:field attributes. th:field should only appear on the select.
<select id="role" class="form-control" th:field="*{role}" >
<option value="USER">USER</option>
<option value="ADMIN" >ADMIN</option>
<option value="SUPERUSER">SUPERUSER</option>
</select>
I did something like following to get it working.
<script th:inline="javascript">
/*<![CDATA[*/
window.addEventListener("DOMContentLoaded", function() {
var optionSelected = [[${product.role}]];
if(optionSelected == null){
$("#role").val("ADMIN");
}
});
/*]]>*/
</script>
and removed the selected attribute from the option.

Resources