Issue #3306: Disable serialization with Laravel "array" cast

XMLWordPrintableJSON

    • None
    • Needed
    • Hide

       1. What to communicate to the user                                           
                                                                                      
        The array cast on Eloquent models stores values as JSON-encoded strings in  
        MongoDB (via Laravel's isJsonCastable mechanism), which is inconsistent with  
        MongoDB's native BSON format. This behavior is now deprecated.
                                                                                      
        Starting with this release, using 'array' as a cast will emit a deprecation   
        notice when writing a value. Users should migrate before the next major      
        version removes this behavior entirely.                                       
                                                                                    
        Migration path (recommended): Remove the array cast. Without a cast, MongoDB  
        natively stores and retrieves PHP arrays as BSON arrays.
                                                                                      
        Alternative (keep JSON string storage): Replace 'array' with 'json'           
        explicitly.                                                                  
                                                                                      
        Forward compatibility: If a field already holds a native BSON array and the   
        model still uses array cast, reading it back will now work correctly (the    
        fromJson override handles this case).                                         
                                                                                    
        —                                                                          
        2. Examples
                   
        Yes, examples would be very helpful. Suggested content:
                                                                                      
        Before (deprecated):

        protected $casts = [                                                          
            'options' => 'array', // stores as JSON string in MongoDB - deprecated  
        ];                      

                                                           
                                                                                      
        After - option 1 (native BSON, recommended):

        protected $casts = [                                                          
            // Remove the cast entirely - MongoDB stores arrays natively            
        ];                                                                            

                                                                                      
        After - option 2 (keep JSON string storage):                                 

        protected $casts = [                                                          
            'options' => 'json', // explicit: still stores as JSON string             
        ];                                                                           

                                                                                      
        It would also be useful to show the deprecation notice output and note that 
        existing JSON-encoded string data in MongoDB is not automatically migrated -  
        users need to migrate their data separately before removing the array cast.
                                                                                      
        —                                                                         
        3. Versions                                                                  
                   
      Laravel-MongoDB 5.8.0              
         

      Show
       1. What to communicate to the user                                                                                                                               The array cast on Eloquent models stores values as JSON-encoded strings in     MongoDB (via Laravel's isJsonCastable mechanism), which is inconsistent with     MongoDB's native BSON format. This behavior is now deprecated.                                                                                    Starting with this release, using 'array' as a cast will emit a deprecation      notice when writing a value. Users should migrate before the next major         version removes this behavior entirely.                                                                                                                         Migration path (recommended): Remove the array cast. Without a cast, MongoDB     natively stores and retrieves PHP arrays as BSON arrays.                                                                                    Alternative (keep JSON string storage): Replace 'array' with 'json'              explicitly.                                                                                                                                                      Forward compatibility: If a field already holds a native BSON array and the      model still uses array cast, reading it back will now work correctly (the       fromJson override handles this case).                                                                                                                           —                                                                             2. Examples                 Yes, examples would be very helpful. Suggested content:                                                                                    Before (deprecated):   protected $casts = [                                                                 'options' => 'array' , // stores as JSON string in MongoDB - deprecated     ];                                                                                                                                                                After - option 1 (native BSON, recommended):   protected $casts = [                                                                 // Remove the cast entirely - MongoDB stores arrays natively               ];                                                                                                                                                                After - option 2 (keep JSON string storage):                                    protected $casts = [                                                                 'options' => 'json', // explicit: still stores as JSON string                ];                                                                                                                                                               It would also be useful to show the deprecation notice output and note that    existing JSON-encoded string data in MongoDB is not automatically migrated -     users need to migrate their data separately before removing the array cast.                                                                                    —                                                                            3. Versions                                                                                 Laravel-MongoDB 5.8.0                  
    • None
    • None
    • None
    • None
    • None
    • None

      alanondra has created Issue #3306: Model `array` cast saved as string in laravel-mongodb. This Jira ticket was filed by GromNaN

      Issue Text:

      • Laravel-mongodb Version: 5.2.0
      • PHP Version: 8.2.27
      • Database Driver & Version: 6.0.20

      Description:

      Steps to reproduce

      1. Create Model with property in `casts` method defined as `array`
      2. Save Model
      3. Query Model from Collection in Studio 3T

      Expected behaviour

      I expected it to save as a MongoDB array.

      Actual behaviour

      It saved as a PHP string representation of an array.

      <details><summary><b>Logs</b>:</summary>
      Insert log.txt here (if necessary)
      </details>

            Assignee:
            Jérôme Tamarelle
            Reporter:
            TPM Jira Automations Bot
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: