I am trying to work through the below code and it is giving me the error as above in postman. I thought that by storing AttendanceRecord in a variable should’ve sufficed. I wonder what did I do wrong ? Below are the function in my UserProfileController, route in api.php and web.php. Please help.
Screenshot of my table in MySQL and migration table code:
https://paste.pics/f6103e8822c01f26bd23d500e0b0e9ad
https://paste.pics/25f8fb016d87113211abfa47e18055d4
https://paste.pics/008df0256fd5ec20caff3772194cc0cf
https://paste.pics/66cca657a49944e8087451c3b75967d3
public function userClockOut(Request $r, $tt, $tt2)
{
$result = [];
$result['status'] = false;
$result['message'] = "something error";
$users = User::where('staff_id', $r->staff_id)->select(['staff_id', 'date_checkIn', 'time_checkOut', 'location_checkOut'])->first();
$tt = AttendanceRecord::find($tt);
$tt2 = AttendanceRecord::find($tt2);
$tz = $tt->created_at;
$tz2 = $tt2->updated_at;
$date = Carbon::createFromFormat('Y-m-d', $tz, 'UTC');
$time = Carbon::createFromFormat('H:i:s', $tz2, 'UTC');
$date->setTimezone('Asia/Singapore');
$time->setTimezone('Asia/Singapore');
$users->date_checkIn = $date;
$users->time_checkOut = $time;
$users->location_checkOut = $r->location_checkOut;
// Save the updated data to the database
AttendanceRecord::updateOrCreate(
['staff_id' => $users->staff_id, 'date_checkIn' => $date],
$users->toArray()
);
$result['data'] = $users;
$result['status'] = true;
$result['message'] = "suksess add data";
return response()->json($result);
}
Route in api.php
Route::post('login', [AuthController::class, 'login']);
Route::get('getdata', [UserProfileController::class, 'getdata']);
Route::post('getdata/{id}', [UserProfileController::class, 'showdata']);
Route::post('adduser', [UserProfileController::class, 'adddata']);
Route::delete('deleteuser', [UserProfileController::class, 'deleteuser']);
Route::PUT('updateuser', [UserProfileController::class, 'updateuser']);
Route::post('updateuserClockIn', [UserProfileController::class, 'userClockIn']);
Route::post('updateuserClockOut', [UserProfileController::class, 'userClockOut']);
Route::middleware('auth:sanctum')->get('user', function (Request $request) {
return $request->user();
});
Route in web.php
<?php
use IlluminateSupportFacadesRoute;
use IlluminateSupportFacadesArtisan;
use AppHttpControllersUserController;
use AppHttpControllersDataFeedController;
use AppHttpControllersDashboardController;
Route::redirect('/', 'login');
// Register new staff
Route::get('register', [UserController::class, 'create'])->name('register');
Route::post('register', [UserController::class, 'store']);
// User list, view profile and update profile
Route::get('users', [UserController::class, 'index']);
Route::get('users/profile/{id}', [UserController::class, 'show']);
Route::get('users/profile/edit/{id}', [UserController::class, 'edit']);
Route::post('users/profile/edit/{id}', [UserController::class, 'update']);
Route::get('history', [UserController::class, 'history']);
Route::middleware(['auth:sanctum', 'verified'])->group(function () {
// Route for the getting the data feed
Route::get('/json-data-feed', [DataFeedController::class, 'getDataFeed'])->name('json_data_feed');
Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');
Route::fallback(function () {
return view('pages/utility/404');
});
});
2
Answers
Based on the way you define the method, you should revise your route as follows:
And in postman you can perform a post request like this (example):
http:://yoursite/updateuserClockOut/13/102
Or to keep the route the same but send the parameters as request parameters, you can do this:
when you use vars in controller method, laravel considers this vars as url segments!
it means that your route must change into:
or if maybe you change them in method to have default values:
or even nullable them in route: