Laravel Edit Form - Select default selection - laravel-5

I'm creating an edit form in Laravel and I would like to select the selected (database) value in Select Dropdown. I'm doing it now as below. Is the right method or anything better is possible? I'm using Laravel 5.4.
<select class="form-control" id="type" name="type">
<option value="admin" #if($user->type==='admin') selected='selected' #endif>Admin</option>
<option value="telco" #if($user->type==='telco') selected='selected' #endif>Telco</option>
<option value="operator" #if($user->type==='operator') selected='selected' #endif>Operator</option>
<option value="subscriber" #if($user->type==='subscriber') selected='selected' #endif>Subscriber</option>
</select>

I would say that this is a normal approach.
If you want to make it more "friendly" it would take some overhead, for example using a #foreach loop to loop an array that would create all the options for the select, something like this :
$arr = ['admin','telco','operator','subscriber'];
#foreach($arr as $item)
<option value="{{ $item }}" #if($user->type=== $item) selected='selected' #endif> {{ strtoupper($item) }}</option>
#endforeach

You can also use ternary operator instead of if
<select class="form-control" id="type" name="type">
<option value="admin" {{($user->type ==='admin') ? 'selected' : ''}}> Admin </option>
<option value="telco" {{($user->type ==='telco') ? 'selected' : ''}}> Telco </option>
<option value="operator" {{($user->type ==='operator') ? 'selected' : ''}}> Operator </option>
<option value="subscriber" {{($user->type ==='subscriber') ? 'selected' : ''}}> Subscriber </option>
</select>

Related

Is there a way I can keep the option I selected in the display?

I have the below drop-down list and would like the month I click on to hold, but as soon as I click Submit it reverts back to January even though its showing me data for a different month.
is there a way the month I'm viewing can be displayed?
The dropdown code
<form class="form-horizontal" method="POST" action="{{ route('timesheet.filter') }}">
#csrf
<div class="form-group">
<label for="">Select Month:</label>
<select name="month" class="form_control">
<option value="01">January</option>
<option value="02">February</option>
<option value="03">March</option>
<option value="04">April</option>
<option value="05">May</option>
<option value="06">June</option>
<option value="07">July</option>
<option value="08">August</option>
<option value="09">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
<input type="submit" value="Submit">
</form>
</div>
Thank you for any help you can give
Added (I realise they are not in date order, this will be another thing to work on) :/
Your question is not so clear,
you can try the selected option for the default value as
<select name="month" class="form_control">
<option value="01" >January</option>
<option value="02" >February</option>
<option value="03" >March</option>
<option value="04" >April</option>
<option value="05" >May</option>
<option value="06" Selected>June</option>
<option value="07" >July</option>
<option value="08" >August</option>
<option value="09" >September</option>
<option value="10" >October</option>
<option value="11" >November</option>
<option value="12" >December</option>
</select>
if you are redirecting with your old valuers when there is a validation error
You can try out the old function as
<select name="month" class="form_control">
<option value="01" {{ old("month")=="01"?'selected':'' }}>January</option>
<option value="02" {{ old("month")=="02"?'selected':'' }}>February</option>
<option value="03" {{ old("month")=="03"?'selected':'' }}>March</option>
<option value="04" {{ old("month")=="04"?'selected':'' }}>April</option>
<option value="05" {{ old("month")=="05"?'selected':'' }}>May</option>
<option value="06" {{ old("month")=="06"?'selected':'' }}>June</option>
<option value="07" {{ old("month")=="07"?'selected':'' }}>July</option>
<option value="08" {{ old("month")=="08"?'selected':'' }}>August</option>
<option value="09" {{ old("month")=="09"?'selected':'' }}>September</option>
<option value="10" {{ old("month")=="10"?'selected':'' }}>October</option>
<option value="11" {{ old("month")=="11"?'selected':'' }}>November</option>
<option value="12" {{ old("month")=="12"?'selected':'' }}>December</option>
</select>
or else if you have to get the current month selected
<?php
$now = Carbon\Carbon::now();
$month = $now->format('m');
?>
<select name="month" class="form_control">
<option value="1" {{ $month=="1"?'selected':'' }}>January</option>
<option value="2" {{ $month=="2"?'selected':'' }}>February</option>
<option value="3" {{ $month=="3"?'selected':'' }}>March</option>
<option value="4" {{ $month=="4"?'selected':'' }}>April</option>
<option value="5" {{ $month=="5"?'selected':'' }}>May</option>
<option value="6" {{ $month=="6"?'selected':'' }}>June</option>
<option value="7" {{ $month=="7"?'selected':'' }}>July</option>
<option value="8" {{ $month=="8"?'selected':'' }}>August</option>
<option value="9" {{ $month=="9"?'selected':'' }}>September</option>
<option value="10" {{ $month=="10"?'selected':'' }}>October</option>
<option value="11" {{ $month=="11"?'selected':'' }}>November</option>
<option value="12" {{ $month=="12"?'selected':'' }}>December</option>
</select>
You can do like below
#php
$months=[
"01"=>"January",
"02"=>"February",
"03"=>"March",
"04"=>"April",
"05"=>"May",
"06"=>"June",
"07"=>"July",
"08"=>"August",
"09"=>"September",
"10"=>"October",
"11"=>"November",
"12"=>"December"
];
#endphp
<select name="month" class="form_control">
#foreach($months as $key=>$value)
<option value="{{$key}}" {{(old('month') ==$key ? 'selected' : '')}}>{{$value}}</option>
#endforeach
</select>
Carbon is your friend here:
<select name="month" class="form-control">
#for($i = 1; $i <= 12; $i++)
#php $selected = (old('month', $month) == $i ? 'selected' : ''); #endphp
<option value="{{ $i }}" {{ $selected }}>
{{ (new Carbon\Carbon())->month($i)->format('F') }}
</option>
#endfor
</select>
Using $i to represent the Month (1-12), you can construct and format a Carbon instance to return "January" through "December". Remeber to keep your code "DRY" - Don't Repeat Yourself.
This requires you to either set $month in the GET request, or perform a return back()->withInput() on the POST request. For example:
ExampleController.php
public function show(){
$month = 1;
// $month = Carbon::now()->format('m')
// $month = Model::first()->month;
// etc.
return view('view')->with(['month' => $month]);
}
public function store(Request $request){
// Do something with `$request->input('month') ...
return back()->withInput();
// return redirect()->route('path_to_view_method');
// etc.
}
Doing the above will ensure a pre-selected value (either 1, the current month or the stored month from the Database) will be selected when you load the view with the dropdown.

Old value in multiple select option in laravel

Here is my select option
<select name="category[]" id="categories"
class="js-example-basic-single form-control"multiple>
#foreach ($category as $element)
<option value=" {{$element->id}}}"{{ (old("category[]") == $element->id ? "selected":"") }}>
{{$element->name}}
</option>
#endforeach
</select>
I can't get the old value ,What I'm doing wrong?
how can i get the old value ?
thanks.
The variable is not named category[] on the server side, that is just notation to turn it into an array; it is named category.
As Kamlesh Paul has stated you can use in_array to check against the possible values of the old input array:
{{ in_array($element->id, (array) old('category', [])) ? "selected" : "" }}
You could try the following code:
<select name="category[]" id="categories" class="js-example-basic-single form-control"multiple>
#foreach ($category as $element)
<option value=" {{$element->id}} " {{ (collect(old('category'))->contains($element->id)) ? 'selected':'' }}>{{ $element->name }}</option>
#endforeach
</select>
you can try in_array()
https://www.w3schools.com/php/func_array_in_array.asp
<select name="category[]" id="categories" class="js-example-basic-single form-control" multiple>
#foreach ($category as $element)
<option value=" {{$element->id}}}" {{ in_array($element->id ,old("category",[]) ? "selected":"") }}>
{{$element->name}}
</option>
#endforeach
</select>

topic.blade.php cannot fetch value->id, but it works with value->name

I got the code snippet,
<div class="form-group">
<label for="category-id-field">Category</label>
<select class="form-control" name="category_id" id="category-id-field">
<option value="" hidden disabled {{ $topic->id ? '' : 'selected' }}>Choose your category</option>
#foreach ($categories as $value)
#php
$selected = $topic->category_id === $value->id ? 'selected' : '';
#endphp
<option value="{{ $value->id }}" {{ $selected }}>{{ $value->name }}</option>
#endforeach
</select>
</div>
And it shows like below, as you can see $value->name works but $value->id just shows up zero.
<div class="form-group"><label for="category-id-field">Category</label>
<select name="category_id" id="category-id-field" class="form-control">
<option value="" selected="selected">Choose your category</option>
<option value="0">statement</option>
<option value="0">life</option>
<option value="0">work</option>
<option value="0">study</option>
</select>
</div>
Versions:
laravel/framework: "^7.0"
Platform: macos catalina
mysql: 8.0.19
Some more background:
Things I did, the id in the categories table used to be integer, I changed it to VARCHAR(30) and added it as the foreign key to table topics->category_id.
If anyone happens to have some experience on this one, please point out the error. Many thanks.

Predefined redundant values in view Laravel

I have here a simple modal that edits user privileges. I want to show the current value of the selected user first but it seems my code is redundant in nature as it repeats. These values are not in a separate table by the way. Is there any simpler way to do this in this view?
<div class="form-group col-md-4">
<label for="user_type">Privilege</label>
<select name="user_type" class="form-control" id="">
<?php $user_type = $user->user_type ?>
#if ($user_type == 'Admin')
<option value="Admin" selected>Admin</option>
<option value="Facilitator">Facilitator</option>
<option value="TeamLeader">TeamLeader</option>
<option value="ScrumLeader">ScrumLeader</option>
<option value="Member">Member</option>
#elseif ($user_type == 'Facilitator')
<option value="Admin">Admin</option>
<option value="Facilitator" selected>Facilitator</option>
<option value="TeamLeader">TeamLeader</option>
<option value="ScrumLeader">ScrumLeader</option>
<option value="Member">Member</option>
#elseif ($user_type == 'TeamLeader')
<option value="Admin">Admi n</option>
<option value="Facilitator">Facilitator</option>
<option value="TeamLeader" selected>TeamLeader</option>
<option value="ScrumLeader">ScrumLeader</option>
<option value="Member">Member</option>
#elseif ($user_type == 'ScrumLeader')
<option value="Admin">Admin</option>
<option value="Facilitator">Facilitator</option>
<option value="TeamLeader">TeamLeader</option>
<option value="ScrumLeader" selected>ScrumLeader</option>
<option value="Member">Member</option>
#else
<option value="Admin">Admin</option>
<option value="Facilitator">Facilitator</option>
<option value="TeamLeader">TeamLeader</option>
<option value="ScrumLeader">ScrumLeader</option>
<option value="Member" selected>Member</option>
#endif
</select>
</div>
I would suggest using the laravelcollective/html package (here)
You will then be able to write something like:
{{Form::select('user_type', $usertypearray, $user->user_type, ['class' => 'form-control']) }}
Where $usertypearray is a value => label array. For instance:
$usertypearray = ['admin'=> 'Admin', 'facilitator'=> 'Facilitator', 'teamleader'=> 'TeamLeader', 'scrumleader'=> 'ScrumLeader','member'=>'Member'];
(I put values in lowercase, but check if it's what the server is looking for)
You can looping on select tag like this
{{ Form::select('user_type',$user_type ,old('user_type'),['class' => 'form-control']) }}
Hope this helps :)

How to create dropdown in laravel

i want dropdown of one column in Laravel, for creating it should display all nations in drop-down, for update purpose current nation should be selected.
any example or tutorial would be helpful thanks.
My Try:
<select name="nation" class="custom-select" >
<option selected value="">Choose...</option>
<option value="{{#$teacher->nation}}" {{#$teacher->nation== "Pakistan" ? 'selected' : ''}} >{{#$teacher->nation}}</option>
</select>
Problem: my dropdown is empty.
<select name="nation" class="custom-select">
<option selected value="">Choose...</option>
#foreach($teachers as $teacher)
<option value="{{ $teacher->nation }}" {{ $teacher->nation == "Pakistan" ? 'selected' : '' }} >{{ $teacher->nation }}</option>
#endforeach
</select>
Here is the dropdown structure in laravel
<select class="form-control m-bot15" name="role_id">
#if($roles->count() > 0)
#foreach($roles as $role)
<option value="{{$role->id}}">{{$role->name}}</option>
#endForeach
#else
No Record Found
#endif
</select>
<select name="nation" class="custom-select" >
<option value="">Choose...</option>
#foreach ($teachers as $key => $value)
<option value="{{ $key }}" {{ old('nation') == $key ? 'selected' : ''}}>{{ $value }}</option>
#endforeach
</select>

Resources