1

I have Model with a deeply nested attribute:

$model = Model::create();
$model->nested_prop = ['foo' => ['bar' => ['x' => 1]]];
$model->save();

In another method I want to modify a single value. This does not work, I get "Indirect modification of overloaded property ... has no effect".:

$model = Model::find ($request->model_id);
$model->nested_prop['foo']['bar']['x'] = 2;
$model->save();

Is there a convenient way of updating a single value without constructing the whole nested structure from scratch?

1
  • Does this framework allow dot-notation? { $set: { "nested_prop.foo.bar.x": 2 } } works in Mongo Playground: mongoplayground.net/p/lh7bb6x9-BI. So maybe $model->nested_prop['foo.bar.x'] = 2; ? Commented Oct 23 at 12:51

2 Answers 2

2

I hope These Method Help You :

$model = Model::find($request->model_id);
$nested = $model->nested_prop;
$nested['foo']['bar']['x'] = 2;
$model->nested_prop = $nested;
$model->save();

OR

$model = Model::find($request->model_id);
data_set($model->nested_prop, 'foo.bar.x', 2);
$model->save();
Sign up to request clarification or add additional context in comments.

Comments

0

Updating elements within a nested array of objects:

When dealing with a nested array of objects, you need to identify the specific element within the array to update. This often involves using a query to target the correct element and then employing MongoDB's positional operators or array filters.

1. Using the positional operator ($) for the first matching element:

This method updates the first element in the array that matches the where clause.

Code

use App\Models\YourModel;

YourModel::where('document_id', 'your_document_id')
    ->where('nested_array.field_to_match', 'value_to_match')
    ->update(['nested_array.$.field_to_update' => 'new_value']);

2. Using array filters for specific elements in a nested array (more advanced):

For more complex scenarios where you need to update multiple elements or elements based on conditions within the array itself, array filters provide more flexibility.

Code

use App\Models\YourModel;

YourModel::where('document_id', 'your_document_id')
    ->update(
        [
            '$set' => [
                'nested_array.$[elem].field_to_update' => 'new_value'
            ]
        ],
        [
            'arrayFilters' => [
                ['elem.field_to_match' => 'value_to_match']
            ]
        ]
    );

In this example, $[elem] is the array filter, and elem.field_to_match is the condition that determines which elements in the nested_array will have their field_to_update modified.

Important Considerations:

  • Model Definition:

    Ensure your Laravel Model for MongoDB correctly defines the connection and collection.

  • Data Structure:

    Understand your MongoDB document's nested structure to correctly target the fields you want to update.

  • Error Handling:

    Implement appropriate error handling for scenarios where documents or nested elements might not be found.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.