Controller method does not updates data - php

I have this form group:
<select class="form-control" id="sel1" name="status">
#if((auth()->user()->two_factor_auth) === 'off')
<option value="off">Off</option>
<option value="sms">SMS</option>
#else
<option value="off">Off</option>
<option value="sms">SMS</option>
#endif
</select>
And as the action, I added this to the method submit of ProfileController:
public function submit(Request $request)
{
if($data['status'] === 'off'){
$request->user()->update([
'two_factor_auth' => 'off'
]);
}else{...}
}
And then at the DB,users table, I have added two_factor_auth column and it is set to sms by default.
So if the user selects, off, it should update the column and set off. But now the problem is it does not do that somehow...
So what is going wrong here ? How can I fix this issue ?
I would really appreciate any idea or suggestion from you guys...
Thanks in advance.

Your html need to be replaced, add the selected attribute to the option you find in your DB/object
<select class="form-control" id="sel1" name="status">
#if((auth()->user()->two_factor_auth) === 'off')
<option selected value="off">Off</option>
<option value="sms">SMS</option>
#else
<option value="off">Off</option>
<option selected value="sms">SMS</option>
#endif
</select>
You can also do in in just two lines
#php
$isOff = auth()->user()->two_factor_auth === 'off';
#endphp
<select class="form-control" id="sel1" name="status">
<option #if($isOff) selected #endif value="off">Off</option>
<option #if(!$isOff) selected #endif value="sms">SMS</option>
</select>
As for the controller, maybe you did not post all the code, but try this
public function submit(Request $request)
{
//you should add validation rules here
$user = auth()->user();
$user->status = $request->input('status', 'sms');//second attribute is for default
//you can add other attributes to update here
$user->save();
}

Related

Drop down selection does not update via edit page in Laravel 8

I have a drop down list in my Laravel project that store data in my SQL DB however in my edit page, everything is able to be changed but the drop down menu selection so I am not sure what I am doing wrong
here is my code
Controller:
public function update(Request $request, $energykey)
{
$energy->softwareversion = $request->input('softwaretype') == "Old" ? "Old System" : "New System";
}
view:
<div>
<select name="softwaretype">
<option value="">--Select Type--</option>
<option {{ $energy->softwareversion == 'Old System' ? 'selected':'' }}>Old System</option>
<option {{ $energy->softwareversion == 'New System' ? 'selected':'' }}>New System</option>
</select>
</div>
In your controller you have to just get the option value
public function update(Request $request, $energykey)
{
$energy->softwareversion = $request->input('softwaretype');
}
In your view make these changes so you can get the values in controller
<div>
<select name="softwaretype">
<option value="">--Select Type--</option>
<option value="Old System" {{ $energy->softwareversion == 'Old System' ? 'selected':'' }}>Old System</option>
<option value="New System" {{ $energy->softwareversion == 'New System' ? 'selected':'' }}>New System</option>
</select>
</div>

How to apply a session value to drop down list using blade in Laravel

I've been racking my head and can't seem to find something that will work to apply Laravel session value to selected value for drop-down menu. Here's the code I currently have. Greatly appreciate any help. Thanks.
<select class="form-control ml-2" style="width: 300px;" name="memberNameFilter" id="memberNameFilter">
<option value="-1">Member Name All</option>
#foreach($member_names as $names)
<option value="{{ $names->MemberName }}" #if($names->MemberName == request()->session()->get('memberNameFilter')) selected="selected" #endif>{{ $names->MemberName }}</option>
#endforeach
</select>
Use the sessionĀ helper function
For example, put something in the session
Route::get('/', function () {
session(['foo' => 'bar']);
return view('welcome');
});
And display it like so
<select name="" id="">
<option value="{{ session('foo') }}">{{ session('foo') }}</option>
<option value="baz">Baz</option>
</select>
Result

How to get id of selected value from dropdown and use it in other dropdown in laravel?

I'm newbie at Laravel and on stackoverflow. I'm making an app for registration. I want to show all those ids of section where class_id is selected in upper dropdown.
$cid=DB::table('classses')->get();
$counterr=0;
$sec=DB::table('sections')->where('class_id',$cid)->get();
$counterrr=0;
Dropdown menu for Class
<div class="col-md-6">
<select class="" name="class_id">
<option value="null">Class</option>
#foreach($cid as $cc)
#if($counterr==0)
<option selected="selected" value="{{$cc->id}}">{{$cc->title}}</option>
{$counterr++}}
#else
<option value="{{$cc->id}}">{{$cc->title}}</option>
#endif
#endforeach
</select>
Dropdown menu for Section where I want to get all the values of section where class_id is upper selected
<select section="" name="section_id">
<option value="null">Section</option>
#foreach($sec as $sc)
#if($counterrr==0)
<option selected="selected" value="{{$sc->id}}">{{$sc->title}}</option>
{{$counterrr++}}
#else
<option value="{{$sc->id}}">{{$sc->title}}</option>
#endif
#endforeach
</select>
Please help me in these case. You can freely ask anything if you want to.
You do not need to set a $counter in order to set the first select option to selected. Instead, you can check if the $key == 0:
<select class="" name="class_id">
<option value="null">Class</option>
#foreach($cid as $key => $cc)
<option {{ $key == 0 ? 'selected="selected"' : '' }} value="{{$cc->id}}">{{$cc->title}}</option>
#endforeach
</select>
Here we use a ternary expression to echo out the selected option based on if $key equals 0. If not, it will not echo out anything.

Laravel blade hide select options in foreach loop

Hi I'm trying to show/hide select options in blade view in Laravel.
im passing in the Model ($assessments) to the view:
[{"id":1,"user_id":1,"name":"Appointment 1","created_at":"2018-03-31 00:00:00","updated_at":"2018-03-31 00:00:00"},{"id":2,"user_id":1,"name":"Appointment 2","created_at":"2018-03-31 00:00:00","updated_at":"2018-03-31 00:00:00"}]
In the view I've got:
<select name="assessment" required>
<option selected>Select...</option>
#foreach($assessments as $assessment)
#if ($assessment->name == 'Appointment 1')
<option value="Appointment 1">Appointment 1</option>
#endif
#if ($assessment->name == 'Appointment 2')
<option value="Appointment 2">Appointment 2</option>
#endif
#if ($assessment->name == 'Appointment 3')
<option value="Appointment 3">Appointment 3</option>
#endif
#endforeach
<option value="Follow Up Phone Call">Follow Up Phone Call</option>
<option value="Home Assessment">Home Assessment</option> -->
</select>
If the if statement equals to True, I don't know if you can put a continue to bypass in the foreach loop? otherwise, it displays values mulitple times from loop.
Let's assume that in the controller, you have got your results and put it in $assesments variable. One thing you can do is that you can define a new array and send it to your view:
$assesmentNames = [];
foreach($assesments as $assesment) {
$assesmentNames[$assesment->name] = $assesment->name;
}
return view('myview', compact('assesments', 'assesmentNames'));
Now automatically the duplicates are gone. Then do this in your view:
#foreach($assessmentNames as $assessmentName)
<option value="{{ $assessmentName }}"> {{ $assessmentName }} </option>
#endforeach
your may use "continue" statement in simple php tags within loop if you want to skip the particular iteration

Laravel Search not working with error massage

What i need is use this search which has a selectable dropdown.see the following screenshot.
These selected data are comes under trainee_division which is in registerdetails data table.here is the controller function i`m developing.
$query = $request->search;
$queryType = $request->institute;
$items = DB::table('registerdetails');
if($queryType == 'Operation' || $queryType == 'operation' ){
$items = $items->where('Operation', '=',"%$queryType%");
}
$items = $items->get();
return view('registeredusers.admindivisiondetails')->with('items',$items);
Relevant view is like this
<form action="divisiondetailsSearch" method="post" class="form-inline">
<select name="institute" id="institute">
<option selected="selected" value="Operation">Operation</option>
<option value="NPA">NPA</option>
<option value="BTS-Kurunegala">BTS-Kurunegala</option>
<option value="INOC">INOC</option>
<option value="RNO">RNO</option>
<option value="Implementation">Implementation</option>
<option value="RAN">RAN</option>
<option value="CEE">CEE</option>
<option value="BTS-Jaffna">BTS-Jaffna</option>
<option value="BTS-Colombo">BTS-Colombo</option>
<option value="Transmission">Transmission</option>
<option value="BTS-Rathnapura">BTS-Rathnapura</option>
<option value="IBS">IBS</option>
<option value="NS">NS</option>
<option value="PCN">PCN</option>
<option value="SQ">SQ</option>
<option value="Pro-Transmission">Pro-Transmission</option>
<option value="BTS-Kandy">BTS-Kandy</option>
</select>
<input type="hidden" value="{{ csrf_token() }}" name="_token" />
<input type="submit" name="submit" value="Search">
</form>
i`m getting this error.
can anyone help me to get solved this one please.
change the query to
if($queryType == 'Operation' || $queryType == 'operation' ){
$items = $items->where('traainee_division', 'like',"%$queryType%");
}
Please check you table first it seems like there is no Operation Column in table..
it might be help you.
As per your comment you need like query to traainee_division column
$items = $items->where('traainee_division','like',"%$queryType%");
note :
your matching value with value . you need to match value with table column value using like query above .

Categories