how to use optional parameter in codeigniter

Try this:

public function get_all_expenses($head = null, $date_to= null, $date_from =null)
{
    $filter = [];
    if( $head ) $filter[] = ['cat_id' => $head]; // == is default
    if( $date_to ) $filter[] = ["date <" => $date_to];
    if( $date_from ) $filter[] = ["date >" => $date_from];

    $this->db->select("*,category.name as cat_name,expense.created_at as created_at_expense");
    $this->db->from('expense')->where($filter);

    //more code
}

You cannot use an array where for "IS NULL", you have to supply it as a whole string.

public function get_all_expenses($head = null, $date_to = null, $date_from = null)
{
    $this->db->select("*, category.name as cat_name, expense.created_at as created_at_expense");
    $this->db->from('expense');

    if (!empty($head)) {
        $this->db->where('cat_id', $head);
    } else {
        $this->db->where('cat_id IS NULL');
    }

    if (!empty($date_to) && !empty($date_from)) {
        $this->db->where('date >', $date_from);
        $this->db->where('date <', $date_to);
    }
    
    //more code
}