Add a new attendance record to database and json return user_id null using JWT AUthentication
I have created the attendance record but when i test on POSTMAN, the user_id is null and the scan_in_status is null also.
"success": true,
"user_id": null,
"action": "SCAN_IN",
"scan_in_location": null,
"date": "2018/11/11",
"time": "05:51:06",
"scan_in_status": null
i am using the JWT Authentication and when i add, it should be added the user_id because i am using the login account with user_id existed in but it is null. This is my code below
public function store(Request $request)
//declare variable for taking current date - time
$input = $request->all();
$user_id = request('user_id');
$scan_out_location = request('scan_out_location');
$work_hour = request('work_hour');
$work_hour_total = request('work_hour_total');
$scan_in_location = request('scan_in_location');
$scan_in_status = request('scan_in_status');
//check attendance using user_id
$res = Attendance::select('id')
->where('user_id' , '=' , $user_id)
->where('attendance_date', '=' , DB::RAW('DATE(NOW())'))
->where('user_id' , '!=' , null)
->where('scan_in_time', '!=' , null)
->where('scan_out_time', '=', null)->get()->count();
//set status 0 - Absent, 1 - On time, 2 - Late, 3 - very late, 4 - On leave
$attendance_status = Attendance::where('user_id', '=' , $user_id)
->select(DB::RAW("CASE WHEN TIME(NOW()) >= '08:30:00' AND TIME(NOW()) < '09:00:00'
THEN '2' WHEN TIME(NOW()) > '09:00:00' THEN '3' ELSE '1' END"))
->value('scan_in_status');
if($res > 0)
$res = DB::statement("UPDATE attendances SET scan_out_time = NOW(),
scan_out_location = ? ,work_hour = TIMESTAMPDIFF(MINUTE, scan_in_time, scan_out_time,lunch_break),
work_hour_total = TIMESTAMPDIFF(MINUTE, scan_in_time, scan_out_time)
WHERE user_id = ? AND NOT scan_in_time IS NULL AND
scan_out_time IS NULL", [$user_id, $user_id, $scan_out_location, $work_hour, $work_hour_total]);
return response()->json(
array(
'success' => true,
'user_id' => $user_id,
'action' => 'SCAN_OUT',
'scan_out_location' => $scan_out_location,
'date' => date('Y/m/d'),
'time' => date('H:i:s')
)
);
else
$res = DB::statement("UPDATE attendances
SET scan_in_time = NOW(), scan_in_location = ?, scan_in_status = ?
WHERE attendance_date = DATE(NOW())
AND user_id = ?", [$scan_in_location, $scan_in_status, $user_id]);
return response()->json(
array(
'success' => true,
'user_id' => $user_id,
'action' => 'SCAN_IN',
'scan_in_location' => $scan_in_location,
'date' => date('Y/m/d'),
'time' => date('H:i:s'),
'scan_in_status' => $scan_in_status
)
);
// $attendance = new Attendance();
$input['user_id'] = Auth::user()->user_id;
$input['attendance_date'] = Carbon::now()->format('Y/m/d');
if ($this->user->attendance()->save($input))
return response()->json([
'success' => true,
'attendance' => $attendance
]);
else
return response()->json([
'success' => false,
'message' => 'Sorry, check in attendance could not be added'
], 500);
Should i change the input to New Attendance or anything else that when i use token of my account to check in, it is recognized with the following user_id and added to database. Thank you
php laravel-5
add a comment |
I have created the attendance record but when i test on POSTMAN, the user_id is null and the scan_in_status is null also.
"success": true,
"user_id": null,
"action": "SCAN_IN",
"scan_in_location": null,
"date": "2018/11/11",
"time": "05:51:06",
"scan_in_status": null
i am using the JWT Authentication and when i add, it should be added the user_id because i am using the login account with user_id existed in but it is null. This is my code below
public function store(Request $request)
//declare variable for taking current date - time
$input = $request->all();
$user_id = request('user_id');
$scan_out_location = request('scan_out_location');
$work_hour = request('work_hour');
$work_hour_total = request('work_hour_total');
$scan_in_location = request('scan_in_location');
$scan_in_status = request('scan_in_status');
//check attendance using user_id
$res = Attendance::select('id')
->where('user_id' , '=' , $user_id)
->where('attendance_date', '=' , DB::RAW('DATE(NOW())'))
->where('user_id' , '!=' , null)
->where('scan_in_time', '!=' , null)
->where('scan_out_time', '=', null)->get()->count();
//set status 0 - Absent, 1 - On time, 2 - Late, 3 - very late, 4 - On leave
$attendance_status = Attendance::where('user_id', '=' , $user_id)
->select(DB::RAW("CASE WHEN TIME(NOW()) >= '08:30:00' AND TIME(NOW()) < '09:00:00'
THEN '2' WHEN TIME(NOW()) > '09:00:00' THEN '3' ELSE '1' END"))
->value('scan_in_status');
if($res > 0)
$res = DB::statement("UPDATE attendances SET scan_out_time = NOW(),
scan_out_location = ? ,work_hour = TIMESTAMPDIFF(MINUTE, scan_in_time, scan_out_time,lunch_break),
work_hour_total = TIMESTAMPDIFF(MINUTE, scan_in_time, scan_out_time)
WHERE user_id = ? AND NOT scan_in_time IS NULL AND
scan_out_time IS NULL", [$user_id, $user_id, $scan_out_location, $work_hour, $work_hour_total]);
return response()->json(
array(
'success' => true,
'user_id' => $user_id,
'action' => 'SCAN_OUT',
'scan_out_location' => $scan_out_location,
'date' => date('Y/m/d'),
'time' => date('H:i:s')
)
);
else
$res = DB::statement("UPDATE attendances
SET scan_in_time = NOW(), scan_in_location = ?, scan_in_status = ?
WHERE attendance_date = DATE(NOW())
AND user_id = ?", [$scan_in_location, $scan_in_status, $user_id]);
return response()->json(
array(
'success' => true,
'user_id' => $user_id,
'action' => 'SCAN_IN',
'scan_in_location' => $scan_in_location,
'date' => date('Y/m/d'),
'time' => date('H:i:s'),
'scan_in_status' => $scan_in_status
)
);
// $attendance = new Attendance();
$input['user_id'] = Auth::user()->user_id;
$input['attendance_date'] = Carbon::now()->format('Y/m/d');
if ($this->user->attendance()->save($input))
return response()->json([
'success' => true,
'attendance' => $attendance
]);
else
return response()->json([
'success' => false,
'message' => 'Sorry, check in attendance could not be added'
], 500);
Should i change the input to New Attendance or anything else that when i use token of my account to check in, it is recognized with the following user_id and added to database. Thank you
php laravel-5
add a comment |
I have created the attendance record but when i test on POSTMAN, the user_id is null and the scan_in_status is null also.
"success": true,
"user_id": null,
"action": "SCAN_IN",
"scan_in_location": null,
"date": "2018/11/11",
"time": "05:51:06",
"scan_in_status": null
i am using the JWT Authentication and when i add, it should be added the user_id because i am using the login account with user_id existed in but it is null. This is my code below
public function store(Request $request)
//declare variable for taking current date - time
$input = $request->all();
$user_id = request('user_id');
$scan_out_location = request('scan_out_location');
$work_hour = request('work_hour');
$work_hour_total = request('work_hour_total');
$scan_in_location = request('scan_in_location');
$scan_in_status = request('scan_in_status');
//check attendance using user_id
$res = Attendance::select('id')
->where('user_id' , '=' , $user_id)
->where('attendance_date', '=' , DB::RAW('DATE(NOW())'))
->where('user_id' , '!=' , null)
->where('scan_in_time', '!=' , null)
->where('scan_out_time', '=', null)->get()->count();
//set status 0 - Absent, 1 - On time, 2 - Late, 3 - very late, 4 - On leave
$attendance_status = Attendance::where('user_id', '=' , $user_id)
->select(DB::RAW("CASE WHEN TIME(NOW()) >= '08:30:00' AND TIME(NOW()) < '09:00:00'
THEN '2' WHEN TIME(NOW()) > '09:00:00' THEN '3' ELSE '1' END"))
->value('scan_in_status');
if($res > 0)
$res = DB::statement("UPDATE attendances SET scan_out_time = NOW(),
scan_out_location = ? ,work_hour = TIMESTAMPDIFF(MINUTE, scan_in_time, scan_out_time,lunch_break),
work_hour_total = TIMESTAMPDIFF(MINUTE, scan_in_time, scan_out_time)
WHERE user_id = ? AND NOT scan_in_time IS NULL AND
scan_out_time IS NULL", [$user_id, $user_id, $scan_out_location, $work_hour, $work_hour_total]);
return response()->json(
array(
'success' => true,
'user_id' => $user_id,
'action' => 'SCAN_OUT',
'scan_out_location' => $scan_out_location,
'date' => date('Y/m/d'),
'time' => date('H:i:s')
)
);
else
$res = DB::statement("UPDATE attendances
SET scan_in_time = NOW(), scan_in_location = ?, scan_in_status = ?
WHERE attendance_date = DATE(NOW())
AND user_id = ?", [$scan_in_location, $scan_in_status, $user_id]);
return response()->json(
array(
'success' => true,
'user_id' => $user_id,
'action' => 'SCAN_IN',
'scan_in_location' => $scan_in_location,
'date' => date('Y/m/d'),
'time' => date('H:i:s'),
'scan_in_status' => $scan_in_status
)
);
// $attendance = new Attendance();
$input['user_id'] = Auth::user()->user_id;
$input['attendance_date'] = Carbon::now()->format('Y/m/d');
if ($this->user->attendance()->save($input))
return response()->json([
'success' => true,
'attendance' => $attendance
]);
else
return response()->json([
'success' => false,
'message' => 'Sorry, check in attendance could not be added'
], 500);
Should i change the input to New Attendance or anything else that when i use token of my account to check in, it is recognized with the following user_id and added to database. Thank you
php laravel-5
I have created the attendance record but when i test on POSTMAN, the user_id is null and the scan_in_status is null also.
"success": true,
"user_id": null,
"action": "SCAN_IN",
"scan_in_location": null,
"date": "2018/11/11",
"time": "05:51:06",
"scan_in_status": null
i am using the JWT Authentication and when i add, it should be added the user_id because i am using the login account with user_id existed in but it is null. This is my code below
public function store(Request $request)
//declare variable for taking current date - time
$input = $request->all();
$user_id = request('user_id');
$scan_out_location = request('scan_out_location');
$work_hour = request('work_hour');
$work_hour_total = request('work_hour_total');
$scan_in_location = request('scan_in_location');
$scan_in_status = request('scan_in_status');
//check attendance using user_id
$res = Attendance::select('id')
->where('user_id' , '=' , $user_id)
->where('attendance_date', '=' , DB::RAW('DATE(NOW())'))
->where('user_id' , '!=' , null)
->where('scan_in_time', '!=' , null)
->where('scan_out_time', '=', null)->get()->count();
//set status 0 - Absent, 1 - On time, 2 - Late, 3 - very late, 4 - On leave
$attendance_status = Attendance::where('user_id', '=' , $user_id)
->select(DB::RAW("CASE WHEN TIME(NOW()) >= '08:30:00' AND TIME(NOW()) < '09:00:00'
THEN '2' WHEN TIME(NOW()) > '09:00:00' THEN '3' ELSE '1' END"))
->value('scan_in_status');
if($res > 0)
$res = DB::statement("UPDATE attendances SET scan_out_time = NOW(),
scan_out_location = ? ,work_hour = TIMESTAMPDIFF(MINUTE, scan_in_time, scan_out_time,lunch_break),
work_hour_total = TIMESTAMPDIFF(MINUTE, scan_in_time, scan_out_time)
WHERE user_id = ? AND NOT scan_in_time IS NULL AND
scan_out_time IS NULL", [$user_id, $user_id, $scan_out_location, $work_hour, $work_hour_total]);
return response()->json(
array(
'success' => true,
'user_id' => $user_id,
'action' => 'SCAN_OUT',
'scan_out_location' => $scan_out_location,
'date' => date('Y/m/d'),
'time' => date('H:i:s')
)
);
else
$res = DB::statement("UPDATE attendances
SET scan_in_time = NOW(), scan_in_location = ?, scan_in_status = ?
WHERE attendance_date = DATE(NOW())
AND user_id = ?", [$scan_in_location, $scan_in_status, $user_id]);
return response()->json(
array(
'success' => true,
'user_id' => $user_id,
'action' => 'SCAN_IN',
'scan_in_location' => $scan_in_location,
'date' => date('Y/m/d'),
'time' => date('H:i:s'),
'scan_in_status' => $scan_in_status
)
);
// $attendance = new Attendance();
$input['user_id'] = Auth::user()->user_id;
$input['attendance_date'] = Carbon::now()->format('Y/m/d');
if ($this->user->attendance()->save($input))
return response()->json([
'success' => true,
'attendance' => $attendance
]);
else
return response()->json([
'success' => false,
'message' => 'Sorry, check in attendance could not be added'
], 500);
Should i change the input to New Attendance or anything else that when i use token of my account to check in, it is recognized with the following user_id and added to database. Thank you
php laravel-5
php laravel-5
asked Nov 11 at 5:58
Huy Nguyen
177
177
add a comment |
add a comment |
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53246246%2fadd-a-new-attendance-record-to-database-and-json-return-user-id-null-using-jwt-a%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53246246%2fadd-a-new-attendance-record-to-database-and-json-return-user-id-null-using-jwt-a%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown