Laravel: create a dynamic property

10,121

You could define an accessor for the address property:

class YourClass {
    public function getAddressAttribute()
    {
        return $this->street.", ".$this->city.", ".$this->state." ".$this->zip;
    }
}

Then, $object->address should return what you need. If you want it to be included on the model's array and JSON forms, you'll need to add it to the $appends property of the model.

class YourClass {
    protected $appends = array('address');
    public function getAddressAttribute()
    {
        return $this->street.', '.$this->city.', '.$this->state.' '.$this->zip;
    }
}

EDIT: for setting, you would have to set up a mutator, like so:

public function setAddressAttribute($value)
{
    // assume $this->handlesParsingAddress parses and returns an assoc array
    $parsed = $this->handlesParsingAddress($value);
    $this->street = $parsed['street'];
    $this->city = $parsed['city'];
    $this->state = $parsed['state'];
    $this->zip = $parsed['zip'];        
}
Share:
10,121
ewok
Author by

ewok

Software engineer in the Greater Boston Area. Primary areas of expertise include Java, Python, web-dev, and general OOP, though I have dabbled in many other technologies.

Updated on July 03, 2022

Comments

  • ewok
    ewok almost 2 years

    I have a decent-size codebase built at this point. I have a couple of tables with matching Eloquent models that are storing addresses in the form ### Street, City, ST, xzipx. These are represented in the database by a single varchar field called address.

    Now here's my issue. I want to add a new feature that allows items to be compared by whether they are in the same city, state, etc. The way to do this as my database is currently configured is to tokenize the address string. This is fine, but a little annoying to have to do it every time.

    The ideal solution would be to restructure the database, and associated models, to split the address field into street, city, state, and zip. The only problem there, would be that everywhere else, where I'm currently accessing the address using $model->address, I would have to construct it from the pieces. This happens a lot throughout the code, so even creating a helper function as below:

    public function address(){
      return $this->street.", ".$this->city.", ".$this->state." ".$this->zip;
    }
    

    would mandate replacing all instances of $model->address with $model->address(), which would still be cumbersome. The ideal solution would be to create a dynamic property, like how Laravel creates them using for relationships. Is this possible?

    Or is there a better solution?

  • ewok
    ewok almost 10 years
    Thanks. I'll give it a shot.
  • ewok
    ewok almost 10 years
    follow-up: Is it possible to create an inverse of this, like setAddressProperty(), such that calling $model->address = BLAHBLAHBLAH; $model->save(); will parse what I pass in and store it in the separate fields?
  • derekaug
    derekaug almost 10 years
    see edit, I didn't bother writing the code that would parse the address, but setting up a mutator would allow this to work if you have the code to parse the address
  • ewok
    ewok almost 10 years
    another followup: can I query over this attribute? i.e. Model::where('address','=','whatever');
  • derekaug
    derekaug almost 10 years
    I do not believe you can query directly on this property. I'd either parse the string and do a where for all the new columns, or find a way to concatenate the columns in the where clause and pass in the full address string there (may need to do a whereRaw, really I'm unsure, be wary of SQL injection though).