preselect radio button based on database value in Laravel 7.24 - laravel

I am trying to pre-select a radio button, based on the value present in the database, and so far none of the other Stackoverflow answers could help with my problem.
The problem is, the value present in the database is NOT selected in the form view
Here is my code.
Blade
div class="form-group ">
<label for="gender">Gender:</label>
<br>
<input class="form-check-input" type="radio" name="gender" id="Female" value="Female" {{(old('gender') == Auth::user()->gender) ? 'checked' : '' }}>
<label class="form-check-label" for="Female">
Female
</label>
<input class="form-check-input" type="radio" name="gender" id="Male" value="Male" {{ (old('gender') == Auth::user()->gender) ? 'checked' : '' }}>
<label class="form-check-label" for="Male">
Male
</label>
<input class="form-check-input " type="radio" name="gender" id="Other" value="Other" {{(old('gender') == Auth::user()->gender) ? 'checked' : '' }}>
<label class="form-check-label" for="Other">
Other
</label>
</div>

Try this
<div class="form-group ">
<label for="gender">Gender:</label>
<br>
<input class="form-check-input text-white" type="radio" name="gender" id="Female" value="Female" {{Auth::user()->gender=="Female" ? 'checked' : '' }} >
<label class="form-check-label" for="Female">
Female
</label>
<input class="form-check-input text-white" type="radio" name="gender" id="Male" value="Male" {{Auth::user()->gender=="Male" ? 'checked' : '' }} >
<label class="form-check-label" for="Male">
Male
</label>
<input class="form-check-input text-white" type="radio" name="gender" id="Other" value="Other" {{Auth::user()->gender=="Other" ? 'checked' : '' }}>
<label class="form-check-label" for="Other">
Other
</label>
</div>
Key change
{{Auth::user()->gender=="Female" ? 'checked' : '' }}
Add this in every radio input field. Don't forget to change the value as {=="Male"},etc.

Check the User Model and see if you've added the gender attribute in fillable Array.

Related

How to get select menu session data in another page laravel 8

I can successfully get input session data on another page but I can't get select menu selected data in another page.
This is my first form
<form action="{{ route('admin.create.step.one.post') }}" method="POST">
#csrf
<label for=""> Student Name </label>
<input type="text" name="student_name" value="{{ $report->student_name ?? '' }}">
<br>
<label for=""> Email </label>
<input type="text" name="student_email" value="{{ $report->student_email ?? '' }}">
<br>
<label for=""> Phone number </label>
<input type="text" name="student_phone" value="{{ $report->student_phone ?? '' }}">
<label for=""> Group </label>
<select name="group" id="">
<option value="1" {{ $report->group ?? '' }}> Science </option>
<option value="2" {{ $report->group ?? '' }}> Arts </option>
</select>
<button type="submit"> Preview </button>
</form>
I want to get session data in this page.
I can't get select menu session data.
<form action="">
#csrf
<table>
<td> {{ $report->student_name }} </td>
<td> {{ $report->student_email }} </td>
<td> {{ $report->student_phone }} </td>
<td> {{ $report->group }} </td>
</table>
</form>
As far as I understood from your question you want to select the item within your select input.
You are just missing this line:
{{ $report->group == 1 ? 'selected' :'' }}
<form action="{{route('admin.create.step.one.post')}}" method="POST">
#csrf
<label for="student_name">Student Name</label>
<input type="text" name="student_name" id="student_name" value="{{ $report->student_name}}">
<br>
<label for="student_email">Email</label>
<input type="text" name="student_email" id="student_email" value="{{ $report->student_email}}">
<br>
<label for="student_phone">Phone number</label>
<input type="text" name="student_phone" id="student_phone" value="{{ $report->student_phone}}">
<br>
<label for="group">Group</label>
<select name="group" id="group">
<option value="1" {{ $report->group == 1 ? 'selected' :'' }}>Science</option>
<option value="2" {{ $report->group == 2 ? 'selected' :'' }}>Arts</option>
</select>
<button type="submit">Preview</button>
</form>
Now, if you need are storing in the session() something and not getting it please update your question with your Controller and I will update my answer to help you further.
I see that you want to get the selected item when you submitted and an error appeared so if you want to get the old value
you can do so using old() method in your blade.
{{ old('group') == 1 ? 'selected' :'' }}
<form action="{{route('admin.create.step.one.post')}}" method="POST">
#csrf
<label for="student_name">Student Name</label>
<input type="text" name="student_name" id="student_name" value="{{ $report->student_name}}">
<br>
<label for="student_email">Email</label>
<input type="text" name="student_email" id="student_email" value="{{ $report->student_email}}">
<br>
<label for="student_phone">Phone number</label>
<input type="text" name="student_phone" id="student_phone" value="{{ $report->student_phone}}">
<br>
<label for="group">Group</label>
<select name="group" id="group">
<option value="1" {{ old('group') == 1 ? 'selected' :'' }}>Science</option>
<option value="2" {{ old('group') == 2 ? 'selected' :'' }}>Arts</option>
</select>
<button type="submit">Preview</button>
</form>

how to disable input when the value is empty laravel livewire

i am using livewire , laravel 7. Below is the code. How to make sure the input is readonly when the value is empty?
<div class="form-row">
#foreach($serviceOrder as $o)
<div class="col-md-5 mb-3">
<label class="form-control-label" >Service {{ $loop->iteration }} </label>
<input type="text" wire:model="serviceOrder.{{ $loop->index }}.serv.serviceName" class="form-control " readonly>
</div>
<input type="hidden" wire:model="serviceOrder.{{ $loop->index }}.serv.servicePrice" class="form-control col-sm-1 mb-3" readonly>
<div class="col-md-3 mb-3">
<label class="form-control-label" >Weight/Size</label>
<input type="text" name="" value="" wire:model="serviceOrder.{{ $loop->index }}.weightsize" class="form-control">
</div>
<div class="col-md-3 mb-3">
<label class="form-control-label" >Quantity*opt</label>
<input type="text" name="" value="" wire:model="serviceOrder.{{ $loop->index }}.quantity" class="form-control" #if(!$quantity) readonly #endif >
</div>
#endforeach
</div>
the one i am focusing is this part
<div class="col-md-3 mb-3">
<label class="form-control-label" >Quantity*opt</label>
<input type="text" name="" value="" wire:model="serviceOrder.{{ $loop->index }}.quantity" class="form-control" #if(!$quantity) readonly #endif >
</div>
for the input that has no value, should be " ", it is a nullable variable for quantity
try adding this check in the element:
<input type="text" wire:model="serviceOrder.{{ $loop->index }}.quantity" class="form-control" #if(!$serviceOrder[$loop->index]['quantity']) readonly #endif>

Compact Two Functions in One View In Laravel

In my Controller
public function startQ(Request $request){
$users = question::limit(2)->get();
return view('answerDesk',compact('users'));
}
public function answerQuestions(Request $request){
$answers =$request->all();
return view('answerDesk',compact('answers'));
}
in My View
<form action="getAnswer" method="post" role="search"> #csrf #foreach($users as $answer) <input type="hidden" name="question[]" value="{{$answer->id}}">
<label class="form-check-label" for="inlineRadio1">
<h4 style=""><i class="fas fa-circle"></i>
{{$answer->id}}:{{$answer->question}}
</h4>
</label><br>
<input type="radio" name="answer[{{$answer->question}}]" value="{{$answer->a}}">(A) : <label class="form-check-label" for="inlineRadio1">{{$answer->a}}</label><br>
<input type="radio" name="answer[{{$answer->question}}]" value="{{$answer->b}}">(B) : <label class="form-check-label" for="inlineRadio1">{{$answer->b}}</label><br>
<input type="radio" name="answer[{{$answer->question}}]" value="{{$answer->c}}">(C) : <label class="form-check-label" for="inlineRadio1">{{$answer->c}}</label><br>
<input type="radio" name="answer[{{$answer->question}}]" value="{{$answer->d}}">(D) : <label class="form-check-label" for="inlineRadio1"> {{$answer->d}}</label><br>
<input type="hidden" name="ans[{{$answer->question}}]" value="{{$answer->ans}}"> #endforeach <input type="submit" value="submit">
</form>
I don't know how to use the getAnswer function
Can I Compact Two Functions in Controller To One View In Laravel?
If Yes How can I Do That?

How to add permission for a user on specific page

I have view, edit and delete permissions to a specific page, so I need to get these values:
page_id, user_id, edit, view, delete
I have a form with page name and 3 checkboxes for each page
<div class="container">
<form action="/test" method="POST">
#csrf
<div class="form-group">
#foreach( $tmp as $key=>$val )
<li>
#if( !isset($val['children']) && empty($var['children']))
<b>{{ $val['data']['name'] }}</b>
#else
<b>{{ app()->getLocale() == 'ar' ? $val['data']->ar_name : $val['data']->display_name }}</b>
#endif
#if( isset($val['children']) and !empty($val['children']))
<ul>
#foreach( $val['children'] as $c=>$child)
<li>
{{ $child->name }} :
<div class="form-group">
<input type="hidden" name="user_id" value="{{ $user_id }}">
<input type="hidden" name="page_id" value="{{ $child->name }}">
<label class="checkbox-inline">
<input type="checkbox" name="arr[]" value="view">View
</label>
<label class="checkbox-inline">
<input type="checkbox" name="arr[]" value="edit">Edit
</label>
<label class="checkbox-inline">
<input type="checkbox" name="arr[]" value="delete">Delete
</label>
</div>
</li>
#endforeach
</ul>
#endif
</li>
#endforeach
<br>
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
but when i sent data, i can't get the correct page_id, is there anything else can i do
I solved it by adding $child->id
<div class="form-group">
<input type="hidden" name="user_id" value="{{ $user_id }}">
<input type="hidden" name="page_id[{{$child->id}}]" value="{{ $child->id }}">
<label class="checkbox-inline">
<input type="checkbox" name="view[{{$child->id}}]" value="1">View
</label>
<label class="checkbox-inline">
<input type="checkbox" name="edit[{{$child->id}}]" value="1">Edit
</label>
<label class="checkbox-inline">
<input type="checkbox" name="del[{{$child->id}}]" value="1">Delete
</label>
</div>

How to echo selected radio value in laravel 5.6

I have user_type column in my users table. On my register blade view i would like to show form with different fields according to the user_type.
<div class="form-check form-check-inline mr-4">
<input class="form-check-input" type="radio" name="user_type" id="exampleRadios1" value="0" checked>
<label class="form-check-label" for="exampleRadios1">
I'm a user
</label>
</div>
<div class="form-check form-check-inline mr-4">
<input class="form-check-input" type="radio" name="user_type" id="exampleRadios2" value="1">
<label class="form-check-label" for="exampleRadios2">
I'm a professional
</label>
</div>
<div class="form-check form-check-inline mr-4">
<input class="form-check-input" type="radio" name="user_type" id="exampleRadios3" value="2">
<label class="form-check-label" for="exampleRadios3">
I'm a facility/business
</label>
</div>
I'm trying to do something like this
#if( Request::get('user_type') == 0 )
Show user registration form
#elseif( Request::get('user_type') == 1 )
Show professional user registration form
#elseif( Request::get('user_type') == 2 )
Show business registration form
#endif
But it doesnt work. It only shows show user registration form as output and does not change according to the selection

Resources