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

jgravois's avatar

Address3 Mutator not working because I don't quite get it

We have US companies with city, state and zip fields and international companies using an address3 field in the database (US companies address3 is null). I wanted to simplify addresses by concatenating the 3 US fields into the address3 field for similarity.

I added this as my mutator but I still get no display in the view:

 public function getAddress3Attribute()
    {
        if($this->address3) {
            return $this->address3;
        } else {
            return $this->city . ', ' . $this->state . '  ' . $this->zip;
        }
    }
0 likes
6 replies
Snapey's avatar

looks fine to me. How are you referencing it in the view?

Snapey's avatar

can you dd in your accessor to be sure its actually being hit?

jgravois's avatar

I did a bit of a refactor and THIS works and I can call it like this

<span class="small">Address:</span>
                    {{$company->full_address}}, {{$company->country}} (<a href="https://www.google.com/maps/place/5350+Poplar+Ave,+Memphis,+TN+38119/data=!4m2!3m1!1s0x887f848c7f05a563:0x966146d05ff56fed?sa=X&ved=0ahUKEwiZpPT7odHRAhUpiVQKHYSCB-oQ8gEIGjAA" target="_blank">Google Map</a>)
                </span>
public function getAddress3Attribute($value)
    {
        if($value) {
            return $value;
        } else {
            return $this->attributes['city'] . ', ' . $this->attributes['state'] . '  ' . $this->attributes['zip'];
        }
    } // end function
    public function getFullAddressAttribute($value)
    {
        return $this->attributes['address'] . ' ' . $this->attributes['address2'] . ' '  . $this->attributes['city'] . ', ' . $this->attributes['state'] . '  ' . $this->attributes['zip'];
    } // end function

Now I have to google to figure out how to make the google maps link dynamic.

Thanks for your prodding!!!

Snapey's avatar
Snapey
Best Answer
Level 122

Don't forget PHP's string substitution instead of concatenation sometimes gives more readable code;

 return "{$this->attributes['address']} {$this->attributes['address2']} {$this->attributes['city']}, {$this->attributes['state']} {$this->attributes['zip']}";

Also, decorating your views should not really be a function of your model - you really need a view helper function.

Google maps: https://developers.google.com/maps/documentation/embed/

jgravois's avatar

LOL, I'm on baby steps over here on a Sunday morning ... I am researching View Presenters at the moment to clear out that model ... you are great and patient and I REALLY appreciate your time.

Please or to participate in this conversation.