活跃记录codeigniter

How to change this code to active record in codeigniter?

$q = "SELECT u.token
  FROM user u 
  WHERE u.userid = ?";
  $r = $this->db->query($q, [$id]);

$this->db->close();

return count($r->result_object) == 1 ? $r->row()->token : 0;
$query = $this->db
    ->select('u.token')
    ->from('user u')
    ->where('u.userid', $id)
    ->get();

return ($query->num_rows() == 1)    ?   $query->row()->token    :   0;

You can try this solution for your problem:

function get_count($id){
   $this->db->select('u.token');
   $this->from('user u');
   $this->where('u.userid', $id); 
   $count = $this->db->get()->row(); 
   return (!empty($count) ? $count->token : 0);
}

I Hope it will help you.