skip to Main Content

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 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


  1. Chosen as BEST ANSWER

    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


  2. You should be able to insert data in a table different from the one defined/attached to the current Model. I.e:

    // ...
    
        public function addBlacklist($dataToken)
        {
            $this->db
                ->table('jwtblacklist')
                ->insert($dataToken);
        }
    
    // ...
    

    Inserting Data

    $builder->insert(?array $set = null, ?bool $escape = null);

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search