Search code examples
phpmysqlcodeigniter-4

Unknown column 'token' in 'field list' Code Igniter 4


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.

dd($dataToken)

Table structure : table structures 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 App\Models;

use CodeIgniter\Model;
use CodeIgniter\Exceptions;
use CodeIgniter\Exceptions\PageNotFoundException;
use CodeIgniter\API\ResponseTrait;
use CodeIgniter\Controller\HomeController;
use Exception;
use CodeIgniter\View\View;

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);
    }
}

Solution

  • Found it !

    turns out you can't insert data to different table in the same model, because the userModel model is used for inserting data to users table.

    So I created another model that used to insert blacklisted token to jwtblacklist table