If I understand you correctly you want to update the existing attendance that belongs to the user or create a new attendance if the user does not have one.
You can simplify your code:
public function present($id)
{
$user = User::findOrFail($id);
$date = date('Y-m-d');
$Attendance = Attendance::firstOrNew(['User_A_ID' => $user->id, 'Date', $date]);
$Attendance->User_A_ID = $user->id;
$Attendance->Date = $date;
$Attendance->A_sts = '0';
$Attendance->save();
return back();
}
Use findOrFail
to check if the user exists, and then use firstOrNew
to retrieve the existing attendance for today or create a new instance of it, this way you can get rid of your if
statement.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…