Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

bradders's avatar

Indirect modification of overloaded property ... has no effect

Howdy!

I'm having trouble with some appended attributes on a model. I get the following error:

Indirect modification of overloaded property App\ModelName::$aspects has no effect

The model itself has the appended attributes, like so:

<?php
class ModelName extends Model {

  protected $appends = ["aspects"];

  public function getAspectsAttribute() {
    // returns an array, or null
  }

}

The error is thrown in a template, where I'm simply looping through the attributes:

@if($problem->aspects)
  @foreach( $problem->aspects as $aspect )
    {{ $aspect->description }}@if ($aspect != end($problem->aspects)),@else.@endif
  @endforeach
@endif

Any ideas on why this is throwing an error? Strangely, this template is an email, and I only see the error when I try and send it. When I view the template in a browser, it's fine.

0 likes
3 replies
bradders's avatar

Thanks for those links, @vilfago however neither seemed to solve my issue.

Maybe I need to rephrase my question. As perhaps I'm using get/set methods wrong?!

The DB table associated with this model has two columns: problem_aspects and problem_elevationswhich hold json data, which is then cast as an array.

I've got two appended get attributes, aspects and elevations. Aspects being the one currently throwing an error when I try and use it.

Is there a better way to append attributes to my model so I can use them? Here's some more info on the model itself:

<?php

class ForecastProblem extends Model {

  protected $appends = ["aspects", "elevations"];
  
  protected $casts = [
    'problem_elevations' => 'array',
    'problem_aspects' => 'array'
  ];

  public function getAspectsAttribute() {

    if(!$this->problem_aspects) {
      return null;
    }

    $problem_aspects = [];

    foreach($this->problem_aspects as $key=>$is_problem_aspect) {
      if($is_problem_aspect == true) {
        $problem_aspects[] = getAspectById($key);
      }
    }

    return $problem_aspects;
  }

  public function getElevationsAttribute() {

    $problem_elevations = [];

    if($this->problem_elevations) {

      foreach($this->problem_elevations as $key=>$is_problem_elevation) {
        if($is_problem_elevation == true) {
          $problem_elevations[] = getElevationById($key);
        }
      }

    }

    return $problem_elevations;
  }

}

bradders's avatar

Hilariously, I just got this error message (again), three years later! A quick google search brought me to my own thread.

Just to close the loop for anyone (and maybe myself in three years time) I refactored the following line to use $loop->last' instead: `` @if ($aspect != end($problem->aspects))```

5 likes

Please or to participate in this conversation.