Chaining of funtion calls

How do you do method chaining in PHP? See how many lines of code I need to write for this?


    public function inboxTotal($userId)
    {
        $q = $this->db->query(
            'SELECT COUNT(*) AS count
            FROM message
            WHERE recipientId = ?',
            array($userId)
        );
        
        $result = $q->result();
        $row = $result[0];
        
        return $row->count;
        
    }

I’d rather do the following, but I always get an error saying whatever is not a property whatever.


    public function inboxTotal($userId)
    {
       return $this->db->query(
            'SELECT COUNT(*) AS count
            FROM message
            WHERE recipientId = ?',
            array($userId)
        )->result()[0]->count;
   }

This kind of stuff can be easily done in JS. I wish PHP were just like this.