I am trying to store historical data from front-end(flutter) through the API. Below are the laravel controller. However, when I ran it on postman, an error comes out Column not found: 1054 Unknown column 'images' in 'field list' . I think it is retrieving the whole column of users. From the column, I only what staff_id, date_checkIn, time_checkIn, and location_checkIn. How do I fix this ?
I tried User::where('staff_id', 'date_checkIn', 'time_checkIn', 'location_checkIn')->first()->toArray();
. but then it says
Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'staff_id = ? limit 1' at line 1 (SQL: select * from users where location_checkIn staff_id = date_checkIn limit 1) in file .....
public function userClockIn(Request $r) {
$result = [];
$result['status'] = false;
$result['message'] = "something error";
$users = User::where('staff_id', $r->staff_id)->first();
// retrieve current data
$currentData = $users->toArray();
// store current data in historical data table
$historicalData = new HistoricalData();
$historicalData->fill($currentData);
$historicalData->save();
$mytime = Carbon::now();
$time = $mytime->format('H:i:s');
$date = $mytime->format('Y-m-d');
$users->date_checkIn = $date;
$users->time_checkIn = $time;
$users->location_checkIn = $r->location_checkIn;
$users->save();
$result['data'] = $users;
$result['status'] = true;
$result['message'] = "suksess add data";
return response()->json($result);
}
CodePudding user response:
To fix this error, you need to specify the columns you want to retrieve from the users table in the where clause. You can do this by using the select method and passing in an array of the columns you want to select.
Here is an example of how you can update your code to fix the error:
$users = User::where('staff_id', $r->staff_id)->select(['staff_id', 'date_checkIn', 'time_checkIn', 'location_checkIn'])->first();
This will only retrieve the specified columns from the users table and will not cause the error.