Predefined redundant values in view Laravel - 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 :)

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.

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.

Laravel Edit Form - Select default selection

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>

Vuejs v-for not populating the select option value in laravel blade

The expected result should return the id's value
<select class="form-control" name="uploaded_segment_id" id="uploaded_segment_id" required="">
<option value="">Choose Segment</option>
<option v-for="uploaded_segment in uploaded_segments" value="#{{ uploaded_segment.id }}"> #{{ uploaded_segment.name }}</option>
</select>
//Output
<option value="">Choose Segment</option>
<option value="{{ uploaded_segment.id }}">Brand1_April_2017_April_2017</option>
According to the documentation, this is the way to do it:
<option v-for="option in options" v-bind:value="option.value">
{{ option.text }}
</option>
So change this line:
<option v-for="uploaded_segment in uploaded_segments" value="#{{ uploaded_segment.id }}"> #{{ uploaded_segment.name }}</option>
to:
<option v-for="uploaded_segment in uploaded_segments" v-bind:value="uploaded_segment.id"> #{{ uploaded_segment.name }}</option>

Laravel Form::select generation in blades template

This line of code:
{!! Form::select('fightingAreas', [1,2,4,8], old('fightingAreas'),['class' => 'form-control']) !!}
Generates :
<select class="form-control" id="fightingAreas" name="fightingAreas">
<option value="0" selected="selected">1</option>
<option value="1">2</option>
<option value="2">4</option>
<option value="3">8</option>
</select>
I would like to generate a value that is equals to text:
<select class="form-control" id="fightingAreas" name="fightingAreas">
<option value="1" selected="selected">1</option>
<option value="2">2</option>
<option value="4">4</option>
<option value="8">8</option>
</select>
Is it posible to do it with Form::select()???
Its actually easier than you think
{!! Form::select('fightingAreas',
['1'=>1,'2'=>2,'4'=>4,'8'=>8],
null,['class' => 'form-control'])
!!}
Also using null will automatically select the old value no need to set old('fightingAreas')
For more info have a look at
http://laravel-recipes.com/categories/21
https://laravelcollective.com/docs/5.2/html
Thanks

Resources