So I’m making a function to blacklist an used token when user logout and save it to my database table, When I trying to insert the data to the table it always says Unknown column 'token' in 'field list'
although the column is exists in the table. The error is basically telling me there is no token
column in my table, and I already check if the value is passed correctly with vardump
and it passed correctly to the function.
Table structure :
How do I fix it ?
Logout Function in Controller:
public function logout()
{
$model = new UserModel();
$currentCookie = $_COOKIE['COOKIE-SESSION'];
$dataToken = [
'token' => $currentCookie,
'logout_at' => date("Y-m-d H:i:s")
];
$model->addBlacklist($dataToken);
setcookie('COOKIE-SESSION', null);
return redirect()->to('/login');
}
Insert Model in UserModel :
<?php
namespace AppModels;
use CodeIgniterModel;
use CodeIgniterExceptions;
use CodeIgniterExceptionsPageNotFoundException;
use CodeIgniterAPIResponseTrait;
use CodeIgniterControllerHomeController;
use Exception;
use CodeIgniterViewView;
class UserModel extends Model
{
use ResponseTrait;
protected $table = 'users';
protected $primaryKey = 'id';
protected $allowedFields = ['username', 'email', 'password', 'token', 'logout_at'];
public function addBlacklist($dataToken)
{
$model = new UserModel();
$builder = $this->table('jwtblacklist');
//dd($dataToken);
$model->save($dataToken);
}
}
2
Answers
Found it !
turns out you can't insert data to different table in the same
model
, because theuserModel
model is used for inserting data tousers
table.So I created another model that used to insert blacklisted token to
jwtblacklist
tableYou should be able to insert data in a table different from the one defined/attached to the current Model. I.e:
Inserting Data
$builder->insert(?array $set = null, ?bool $escape = null);