i am using laravel query builder but the problem is i am using radio buttons but i want to update the attendance at the radio button but it returns an error Undefined variable $attendance_status i don't know why please help and how can i pass the $attendance_status variable
here is my code
my form
<form action="{{route('Attendances.update',$student->id)}}" method="post">
@csrf
@method('PUT')
<input type="hidden" name="id" value="{{$student->id}}">
<label >
<input name="attendences"
{{ $student->attendances()->first()->attendence_status == 1 ? 'checked' : '' }}
type="radio" value="presence">
<span >حضور</span>
</label>
<label >
<input name="attendences"
{{ $student->attendances()->first()->attendence_status == 0 ? 'checked' : '' }}
type="radio" value="absent">
<span >غياب</span>
</label>
<div >
<button type="button"
data-dismiss="modal">{{trans('Students_trans.Close')}}</button>
<button >{{trans('Students_trans.submit')}}</button>
</div>
</form>
here is my controller update
public function update(Request $request, $id)
{
// return $request;
if($request->attendances == 'absent'){
$attendance_status = 0;
}
else if($request->attendances == 'presence'){
$attendance_status = 1;
}
Attendance::find($id)->update([
'student_id'=> $id,
'grade_id'=> $request->grade_id,
'class_id'=> $request->classroom_id,
'section_id'=> $request->section_id,
'attendance_date'=> date('Y-m-d'),
'status' => $attendance_status,
]);
return back();
CodePudding user response:
You have an if
and an elseif
in your Controller, so only 2 conditions would create a variable named $attendance_status
. You probably want to add a default branch, else
basically, to make sure that the variable gets created with some default value before you try to use it in your update
call.
Not sure which one of those 2 options you want to be the default but this would simplify things:
$attendance_status = $request->attendences == 'presence';
CodePudding user response:
https://laravel.com/docs/9.x/redirects#redirecting-with-flashed-session-data
You may use the
withInput
method provided by theRedirectResponse
instance to flash the current request's input data to the session before redirecting the user to a new location. Once the input has been flashed to the session, you may easily retrieve it during the next request:return back()->withInput();
CodePudding user response:
dd($request()->all());
1.Use this and just verify what values you are getting i am pretty sure that you will not getting the value on which you have used if and else if.
- if and else make sense but in your code you are using if else if but else part is missing
CodePudding user response:
Surely {{ $student->attendances()->first()->attendence_status == 0 ? 'checked' : '' }}
logic does not work in this way.
try this instead
<input name="attendences" checked="{{ $student->attendances()->first()->attendence_status == 0 ? true : false}}" type="radio" value="absent">