Laravel - if condition for select - php

My if condition for selected Input is not showing anyhing. Here is my create.blade.php:
...
{!! Form::open(['action' => 'PostsController#store', 'method' => 'POST']) !!}
<div class="column">
<label for="title">Title</label></br>
<input name="title" type="text" placeholder="" class="form-control">
</div>
</br>
<div class="column">
<label for="brand">Brand</label></br>
<select class="form-control" id="soflow" name="brand">
<option value="" disabled selected>Select your option</option>
<option value="Volkswagen">Volkswagen</option>
<option value="BMW">BMW</option>
<option value="Audi">Audi</option>
</select>
</div>
</br>
#if(Input::has('brand') == 'Volkswagen')
<div class="column">
<label for="model">Model</label></br>
<select class="form-control" id="soflow" name="model">
<option value="" disabled selected>Select your option</option>
<option value="Golf1">Golf 1</option>
<option value="Golf2">Golf 2</option>
<option value="Golf3">Golf 3</option>
</select>
</div>
#endif
{!! Form::close() !!}
...
But after this my select for model is not showing at all and I just need to show specific models if select input(brand) is for Volkswagen. Any solutions? Please help. Thank you!

i think the solution for your problem can be chained js https://appelsiini.net/projects/chained/ . I am not sure if this works for you. Let me know if it worked.

Input::has('brand')
shall return T/F if there is an input field with name 'brand' or not. The line,
Input::has('brand') == 'Volkswagen'
in your logic, evaluates to
true == 'Volkswagen'
Try
#if(Input::get('brand')=='Volkswagen')

Related

How to use an if statement on the blade template. error: Undefined variable $category

I have created a function to retrieve a specific column in the database using eloquent, on my productsController
public function create()
{
$category = Product::select('category')->get();
return view('products.create');
}
And Now I want to use an if statement on the create.blade.php to check whether the value in the database is equal to 'liquor',
<div class="row mb-4">
<label for="category" class="col-md-4 col-form-label text-md-end">Category</label>
<div class="col-md-6">
<select name="category" id="category" class="form-select col-md-6">
<option value="liquor">liquor</option>
<option value= "food">food</option>
<option value="DIY">DIY</option>
<option value= "thrift shops">thrift shops</option>
<option value= "home decor">home decor</option>
<option value= "phones and tablets">phones and tablets</option>
<option value= "computing">computing</option>
<option value= "electronics">electronics</option>
<option value= "beauty products">beauty products</option>
<option value= "others">others</option>
</select>
</div>
</div>
#if($category == 'liquor')
<div class="row mb-4">
<label for="subcategory" class="col-md-4 col-form-label text-md-end">Sub Category</label>
<div class="col-md-6">
<select name="subcategory" id="subcategory" class="form-select col-md-6">
<option value="null">Select subcategory</option>
<option value="wine">Wine</option>
<option value="whisky">Whisky</option>
<option value="brandy">Brandy</option>
<option value="scotch">Scotch</option>
<option value="spirit">Spirit</option>
<option value="gin">Gin</option>
<option value="vodka">Vodka</option>
<option value="beer">Beer</option>
<option value="rum">Rum</option>
<option value="mixers">Mixers</option>
<option value="bourbon">Bourbon</option>
<option value="cognac">Cognac</option>
<option value="other">Other</option>
</select>
</div>
</div>
<div class="row mb-4">
<label for="volume" class="col-md-4 col-form-label text-md-end">Volume</label>
<div class="col-md-6">
<select name="volume" id="volume" class="form-select col-md-6">
<option value="null">Choose volume</option>
<option value="5ltr">5ltr</option>
<option value="1ltr">1ltr</option>
<option value="750ml">750ml</option>
<option value="500ml">500ml</option>
<option value="250ml">250ml</option>
<option value="other">Other</option>
</select>
</div>
</div>
#endif
However, I get the following error: Undefined variable $category
What could be the issue?
I think you forgot to send the $category to the view:
return view('products.create', compact("category"));
or like this:
return view('products.create')
->with('category', $category)
->with('title', 'New Title');
or like this:
return view('products.create', ['category' => $category]);
You can add as many vars/collections/or whatever as you wish both ways.
use this
#if(isset($category) && $category == 'liquor')
//your code
#endif
Also check this link for validation
you need to send $category from controller to your blade like this
public function create()
{
$category = Product::select('category')->get();
return view('products.create')->with('category',$category);
}
you can also do this with another way
public function create()
{
$category = Product::select('category')->get();
return view('products.create',compact('category'));
}

Check old data is null or not or check by isset() from input laravel Blade

How can I check old data is available or not after validation from controller?
i used this but it shows error
#if(\Illuminate\Http\Request::old('status')!=null)
<option style="display: none" value="{{old('status')}}" hidden>{{old('status')}}</option>
#endif
also used this one
#if(isset(old('status')))
<option style="display: none" value="{{old('status')}}" hidden>{{old('status')}}</option>
#endif
full form is
<form action="{{route('add-expense-store')}}" method="post" class="new-added-form">
{{ #csrf_field() }}
<div class="row">
<div class="col-xl-8 col-lg-8 col-12 form-group">
<label>Expense Type</label>
<select class="select2" name="type">
<option value="">Please Select</option>
{{--<option value="Teacher's Salary">Teacher's Salary</option>
<option value="Staff's Salary">Staff's Salary</option>--}}
<option value="Transport">Transport</option>
<option value="Mobile Bill">Mobile Bill</option>
<option value="Utility Bill">Utility Bill</option>
<option value="Stationary">Stationary</option>
<option value="Print & Press">Print & Press</option>
<option value="Photocopy & Compose">Photocopy & Compose</option>
<option value="Entertainment & Hospitality">Entertainment & Hospitality</option>
<option value="Donation">Donation</option>
<option value="Program">Program</option>
<option value="Personal or Owner">Personal or Owner</option>
</select>
</div>
<div class="col-xl-4 col-lg-4 col-12 form-group">
<label>Status</label>
<select class="select2" name="status">
#if(isset(old('status')))
<option style="display: none" value="{{old('status')}}" hidden>{{old('status')}}</option>
#endif
<option value="">Please Select</option>
<option value="Paid">Paid</option>
<option value="Due">Due</option>
</select>
</div>
<div class="col-xl-6 col-lg-6 col-12 form-group">
<label>Holder Name</label>
<input type="text" placeholder="" class="form-control" name="holder_name"
value="{{ old('holder_name') }}">
</div>
<div class="col-xl-6 col-lg-6 col-12 form-group">
<label>Phone</label>
<input type="number" placeholder="" class="form-control" name="phone" {{ old('phone') }}>
</div>
<div class="col-12 form-group mg-t-8">
<button type="submit" class="btn-fill-lg btn-gradient-yellow btn-hover-bluedark float-right">Save
</button>
</div>
</div>
</form>
controller
$this->validate($request, [
'type' => 'required',
'status' => 'required'
]);
both are getting error. How can I check old value us available or not in blade?
Now I include my form data. here I wants to hidden if old value is not found. please help with this one. check isset old data in blade.
You can check it in this way. Remember to return it in this way from controller
$validator = $this->validate($request, [
'type' => 'required',
'status' => 'required'
]);
if ( $validator->fails() ) {
return back()->withErrors( $validator )->withInput();
}
In your view do this
<select class="select2" name="status">
<option value="">Please Select</option>
<option value="Paid" {{ old('status') == 'Paid' ? 'selected' : '' }}>Paid</option>
<option value="Due" {{ old('status') == 'Due' ? 'selected' : '' }}>Due</option>
</select>

How can I get the selected index of a select?

I am in Laravel 5.7, I will wish to get the index of a select. I have tried this but without success so far.
<div class="form-group">
<label for="company-content">Ville</label>
<select name="fk_localite" id="" class="form-control">
<option value="">Selectionner votre ville</option>
#foreach($localites as $localite)
<option value="{{$localite->id}}">{{$localite->ville}}
</option>
#endforeach
</select>
Here is the solution !
<div class="form-group">
<label for="company-content">Ville</label>
<select name="fk_localite" id="" class="form-control">
#foreach($localites as $localite)
<option value="{{$localite->id}}" #if($eleves->fk_localite == $localite->id) selected #endif>{{ $localite->ville }}
</option>
#endforeach
</select>
</div>

how to grab optional values in to the edit form in laravel

I am developing laravel application and submit some form data using form.blade.php file
<select name="status" id="status">
<option value="">Choose a status</option>
<option value="Upcoming">Upcoming</option>
<option value="Active">Active</option>
<option value="Completed">Completed</option>
</select>
#if ($errors->has('status'))
<span class="help-block">{{ $errors->first('status') }}</span>
#endif
and now I need edit this data using edit form and this is My edit form and grab values from table,
<select name="assign" id="status">
<option value="{!! $task->assign !!}">{!! $task->assign !!}</option>
</select>
#if ($errors->has('assign'))
<span class="help-block">{{ $errors->first('assign') }}</span>
#endif
this is working but unable to select other optional values in the edit form optional values input. how can I fix this problem?
John, you should use "multiple" attribute for select:
<select name="status" id="status" multiple>
<!-- Options here -->
</select>

Validate select form in laravel

I have this html on my contact form:
<div class="form-group">
<label class="col-md-2 control-label" for="type">Type of website?</label>
<div class="col-md-10 inputGroupContainer">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-file-code-o"></i></span>
<select class="form-control" id="type">
<option>Business</option>
<option>Company</option>
<option>Personal</option>
<option>Online Shopping</option>
<option>Other</option>
</select>
</div>
</div>
</div>
How can I validate and pass data of this field in email?
Currently I have this:
<!-- Text input-->
<div class="form-group">
<label class="col-md-2 control-label">Type</label>
<div class="col-md-10 inputGroupContainer">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-file-code-o"></i></span>
<input name="type" placeholder="eg. Business, Company, Personal, Online Shopping, etc." class="form-control" type="text">
</div>
</div>
</div>
And I validate and pass it like this:
$data = array(
'type' => $request->type,
);
but that's input field what I want is my first sample (select box), so I'm not sure how to do it!
thanks.
First Give THe Name To Select drop down fields, then after Simplest Way Is use in Validation Rules with the name of select input fields. that you give required
Example:
<select class="form-control" id="type" name="Business_Type">
<option value="">Select Type</option>
<option value="Business">Business</option>
<option value="Company">Company</option>
<option value="Personal">Personal</option>
<option value="Online Shopping">Online Shopping</option>
<option value="Other">Other</option>
</select>
Validation rules
// Validation rules somewhere...
$rules = [
...
'Business_Type' => 'required',
...
];
This will work perfectly.
$rules = [
'selType' => 'required|in:Business,Company,Personal,Online Shopping,Others'
];
You can do it like this :
<div class="form-group">
<label class="col-md-2 control-label" for="type">Type of website?</label>
<div class="col-md-10 inputGroupContainer">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-file-code-o"></i></span>
<select class="form-control" id="type" name="typeSelection">
<option>--Select type--</option>
<option>Business</option>
<option>Company</option>
<option>Personal</option>
<option>Online Shopping</option>
<option>Other</option>
</select>
</div>
</div>
</div>
For the rules :
$rules = [
'typeSelection' => 'required|not_in:0'
];
not_in:0 to avoid submission of the first choice witch is --Select type-- :)
And to pass infos to the validation just use $request->all() because it will use the field name in this case typeSelection no need for ctreating a new array !
In Laravel 8 i use this, i found out that if you change the value to an other value trough inspect element 'yes i know this sounds funky' still can be inserted into the validator and pass. Here's the solution
Select:
<select class="form-control" id="type" name="Business_Type">
<option value="">Select Type</option>
<option value="Business">Business</option>
<option value="Company">Company</option>
<option value="Personal">Personal</option>
<option value="Online Shopping">Online Shopping</option>
<option value="Other">Other</option></select>
Validator:
$this->validate($request, [
'selType' => 'required|in:Business,Company,Personal,Online Shopping,Others'
]);
Just enter the values that the validator must pass, all other values will not pass
You need to validate your select with required and for that you need to setup select like:
Select into blade:
<select class="form-control" id="type" name="selType">
<option value="">Select Type</option>
<option value="Business">Business</option>
<option value="Company">Company</option>
<option value="Personal">Personal</option>
<option value="Online Shopping">Online Shopping</option>
<option value="Other">Other</option>
</select>
Validation rules somewhere:
$rules = [
'selType' => 'required'
];
if you want to check if the selected value is exist in your table's column you can check it like this example
<select class="form-control" id="type" name="Business_Type">
<option value="">Select Type</option>
<option value="Business">Business</option>
<option value="Company">Company</option>
<option value="Personal">Personal</option>
<option value="Online Shopping">Online Shopping</option>
<option value="Other">Other</option>
</select>
for above selection
'Business_Type' => 'required|my_business ,type'
This will make sure the selected value exists for example in the my_business table on the type column.
if you select the first option it will return the error
The selected type is invalid.
You should put Value in each option
The first one should be
value=""
and the rest put the values of them like this
<option value="">Select Type</option>
<option value="Business">Business</option>
<option value="Company">Company</option>
<option value="Personal">Personal</option>
<option value="Online Shopping">Online Shopping</option>
<option value="Other">Other</option>

Categories