I want to use JSON Response _id #3241
              
                
                  
                  
                    Answered
                  
                  by
                    GromNaN
                  
              
          
                  
                    
                      savasdersimcelik
                    
                  
                
                  asked this question in
                General
              
            -
| With the example code above, I am returning a response to the mobile application API. Previously, in our mobile application, we accessed it with _id, but now it is all changed to id. My problem is that I want to use _id in the JSON responses.  | 
Beta Was this translation helpful? Give feedback.
      
      
          Answered by
          
            GromNaN
          
      
      
        Jan 7, 2025 
      
    
    Replies: 2 comments 1 reply
-
| You can change the representation of the model to rename  class Invoice
{
    // ...
    public function toArray()
    {
        $values = parent::toArray();
        if (array_key_exists('id', $values)) {
            $values['_id'] = $values['id'];
            unset($values['id']);
        }
        
        return $values;
    }
} | 
Beta Was this translation helpful? Give feedback.
                  
                    0 replies
                  
                
            
      Answer selected by
        jmikola
        
          
            
              This comment was marked as disruptive content.
            
          
            
        
      
    
            
              This comment was marked as disruptive content.
            
          
            
        
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
        
    
You can change the representation of the model to rename
idto_idby overriding thetoArraymethod of your model class: