How to get the old value in a select without unchecked with option formed with an foreach - laravel

I need a little help..
In case of validation errors, when the page is reloaded I lose the information entered previously.
This is my code:
Controller:
public function create()
{
$nationalities = array('Italian','Brazilian','Spanish','Romanian');
return view('guest.create', ['nationalities'=>$nationalities);
}
public function store(Request $request)
{
$request->validate([
'nationality' => 'required'
]);
Guest::create([
'nationality' => $request->nationality
]);
return redirect(route('guest.index'));
}
View:
<div class="mb-3">
<label for="nationality" class="form-label fw-bold">Select<span class="required">*</span></label>
<select class="form-select" id="nationality" name="nationality" aria-label="Floating label select example">
#foreach($nationalities as $nationality)
<option value="{{$nationality}}">{{$nationality}}</option>
#endforeach
</select>
</div>
How can I modify the code so that this doesn't happen?

How can you select back what failed if you did not even ask for the old value?
This may or not work depending on how you are validating, but you did not want to show that...
<div class="mb-3">
<label for="nationality" class="form-label fw-bold">Select<span class="required">*</span></label>
<select class="form-select" id="nationality" name="nationality" aria-label="Floating label select example">
#foreach($nationalities as $nationality)
<option value="{{$nationality}}" {{ old('nationality') === $nationality) ? 'selected="selected"' : '' }}>{{$nationality}}</option>
#endforeach
</select>
</div>

<option value="{{$nationality}}" #selected(old('nationality')==$nationality)>
{{$nationality}}
</option>
add the #selected directive in blade.

Related

How to show query search results without reloading on button click in laravel

I am trying to show query results without reloading in laravel
This is the form in which I get the category and country
<form class="new_is" id="lets_search" action="{{route('advanced-search.show','searchInfluencer')}} method="GET">
<div class="row">
<div class="col-md-6">
<label>Category</label>
<select name="filter_category" id="filter_category" class="influencer-category-picker form-control form-select" >
<option value="">Click to select the category</option>
#foreach($Categories as $Category)
<option id="category" name="{{$Category->id}}" value="{{$Category->id}}">{{$Category->name}}</option>
#endforeach
</select>
</div>
<div class="col-md-6">
<label>Location</label>
<select name="filter_country" id="filter_country" class="influencer-category-picker form-control form-select">
<option value="" >Click to select the Country</option>
#foreach($Countries as $Country)
<option id="country" value="{{$Country->id}}">{{$Country->name}}</option>
#endforeach
</select>
</div>
<div id="advanced-search" class="col-md-12" style="margin-top: 20px">
<p>
<button type="button" name="filter" id="filter" class="btn btn-lg btn-primary track-event">Search</button>
</p>
</div>
</div>
</form>
This is the controller including the query and returning the result and everything works well but on another page
public function searchInfluencer(Request $request)
{
$category = $request->get('category');
$country = $request->get('country');
if(empty($category) && !empty($country)){
$influencers =Influencer::where('country_id', $country)->get();
return response()->json($influencers);
}
elseif (!empty($category) && empty($country)){
$influencers =Influencer::where('category_id', $category)->get();
return response()->json($influencers);
}
elseif (!empty($category) && !empty($country)){
$influencers =Influencer::where('category_id', $category)->where('country_id', $country)->get();
return response()->json($influencers);
}
Here is the index function:
public function index(){
return view('businesses.advanced-search-business', [
'Genders' => Gender::all(),
'Countries' => Country::all(),
'Categories' => Category::all(),
]);
}
The issue is that on clicking the search button, I want to show the results on the same page without reloading. Thank you in advance for your help

Laravel validation with create form: retrieve old input data for dropdown box

I have a Laravel application which uses a form to create items. The form uses validation as defined in the Laravel documentation under this section.
I have a StoreItem Request file:
public function rules() {
return [
'item_name'=>'required',
'category_id'=> 'required',
'collection_id'=> 'required',
'brewery_id'=> 'required',
];
}
and in the controller I have the following:
public function store(storeItem $request)
{
$validated = $request->validated();
$item = new Item([
'user_id' => Auth::id(),
'item_name' => $request->item_name,
'category_id' => $request->category_id,
'collection_id' => $request->collection_id,
'brewery_id' => $request->brewery_id,
]);
$item->save();
When validation is catching an error, the create form is presented again but I don't see the old input data.
For the input text fields, I have done the following:
<input type="text" class="form-control" name="item_name" value="{{ old('item_name') }}" />
and this shows the old input data, but what to do for the dropdown boxes like category_id, collection_id and brewery_id. I want them to have the old value as the 'selected' value.
Currently in the form I have (for the dropdown boxes):
<div class="form-group">
<label class="form-label" for="brewery">Brewery: (*)</label>
<select class="form-control" name="brewery_id" >
#foreach($breweries as $brewery)
<option value="{{ $brewery->id }}">{{ $brewery->brewery_name }}</option>
#endforeach
</select>
</div>
This source seems to indicate that using the old() method might even not be needed, but if I don't use the
You need to manually add the selected attribute to the previously selected value. For example:
<div class="form-group">
<label class="form-label" for="brewery">Brewery: (*)</label>
<select class="form-control" name="brewery_id" >
#foreach($breweries as $brewery)
<option value="{{ $brewery->id }}" {{ old("brewery_id") == $brewery->id ? "selected" : "" }}>{{ $brewery->brewery_name }}</option>
#endforeach
</select>
</div>
try this as well. I've added bracket open and close to (old('brewery_id')==$brewery->id)
<div class="form-group">
<label class="form-label" for="brewery">Brewery: (*)</label>
<select class="form-control" name="brewery_id" >
#foreach($breweries as $brewery)
<option value="{{ $brewery->id }}" {{ (old('brewery_id') == $brewery->id)? "selected" : "" }}>{{ $brewery->brewery_name }}</option>
#endforeach
</select>
</div>

how to validate select box in laravel (without using JS)

<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">Publication Status</label>
<div class="col-sm-10">
<select name="publicationStatus" class="form-control">
<option value="">Select Manufacturer</option>
<option value="1">Published</option>
<option value="0">Unpublished</option>
</select>
<span class="text-danger">{{$errors->has('publicationStatus')? $errors->first('publicationStatus'):''}}</span>
</div>
</div>
my controller
public function storeManufacturer(Request $request){
$this->validate($request,[
'manufacturerName'=>'required',
'manufacturerDescription'=>'required',
'publicationStatus'=>'required',
]);
above 'manufacturerName' & 'manufacturerDescription' is working but publicationStatus is not validate in the same way.Maybe the reason is that contain multiple value.
You could check data of $request with dd() in your controller firstly:
dd($request->publicationStatus);

how to convert select box html to form html collactive

Hi i have this in my controller
$listPatient = Patient::get();
return view('backend.consultations.create')->with([
'patient' => $this->patient,
'patient_id' =>$this->patient_id,
'listPatient' => $listPatient,
]);
and in my view i have
<div class="form-group">
<div class="col-md-2">
List clients
</div>
<div class="col-lg-10">
<select name="patient_id">
<option value="0">Veillier séléctionner un patien </option>
#foreach($listPatient as $key)
<option value="{{$key->id}}">{{$key->nom_patient}} {{$key->prenom_patient}}</option>
#endforeach
</select>
</div><!--col-lg-10-->
</div><!--form control-->
and it work fine,i want to use Form::select but it doesn't work can anyone help me please
In your controller you would want the following to fetch $listPatient
$listPatient = Patient::lists("nom_patient","id")->toArray();
and in your view you would want
{!! Form::select('patient_id', [0 => 'Veillier séléctionner un patien'] + $listPatient, null) !!}
a simple solution
enter code here : <div class ="form-group">
{{ Form::label('list_patient', trans('validation.attributes.backend.consultations.nom_patient'), ['class' => 'col-lg-2 control-label required']) }}
<div class="col-lg-10">
<select id="patient_id" name="patient_id" class="form-control select2 box-size" required="required" placeholder=" Non définie" >
<option value="">Non définie</option>
#foreach($listPatient as $key)
<option value ="{{$key->id}}">{{$key->nom_patient}} {{$key->prenom_patient}}</option>
#endforeach
</select>
</div><!--col-lg-3-->
</div><!--form control-->

Dynamically populated select menus in Laravel5

In a view I have a couple of select menus that are populated by data from two separate tables.
Looking for a way to populate the second select menu depending on the value of the first.
<div class="form-group">
{{Form::label('company', 'Company')}}
<select selected="" class="form-control" id="company" name="company">
#foreach($company as $key)
<option value="">{{$key->name}}</option>
#endforeach
</select>
</div>
<div class="form-group">
{{Form::label('department', 'Department')}}
<select selected="" class="form-control" id="department" name="department">
#foreach($department as $key)
<option =value"">{{$key->name}}</option>
#endforeach
</select>
</div>
This feels like something that shouldn't be too hard, but have yet to find any good information about it.
A way to do this is using javascript. The main idea is to have a partial view with the second select and then call it when the user change something in the first select.
To do this, you need to have two views. The main view will have a call to the second view with a default collection of departments:
<div class="form-group">
{{Form::label('company', 'Company')}}
<select selected="" class="form-control" id="company" name="company">
#foreach($company as $key)
<option value="">{{$key->name}}</option>
#endforeach
</select>
</div>
<div id="second-select" class="form-group">
#include('contents.partial', ['department' => $department])
</div>
The second view (contents.partial) will print the form:
{{Form::label('department', 'Department')}}
<select selected="" class="form-control" id="department" name="department">
#foreach($department as $key)
<option =value"">{{$key->name}}</option>
#endforeach
</select>
Then you'll need a route to call a function to render the second view based on some value:
Route::get('/get/content/{value}', 'ContentController#getContent');
And the function to render the view in ContentController:
public function getContent(Request $request, $value)
{
$department = Department::where('value', $value)->get();
return view('contents.partial', [
'department' => $department
])->render();
}
And to call this, you need a javascript/jquery to identify the change in the select and fire the function in controller
<script>
$(document).ready(function(){
setListener();
});
function setListener() {
$('#company').change(function() {
var value = $(this).val();
$.get( "/get/content/"+value, function( data ) {
$('#second-select').empty();
$('#second-select').append(data);
});
});
}
</script>
This is the main idea, ok?

Resources