Thursday, 8 June 2017

Topic 21 - Laravel Accessor & Mutator

Accessor 

An access function which can be implant inside of our model . For java, we called gettor. Below shows the example of Program Name with the return the string value if were match.
  public function getProgramNameAttribute()
  {
      if ($this->program == '1') {
        return 'Bachelor of Business Admin';
      }
      else if ($this->program == '2') {
        return 'Bachelor of Pure Math';
      }
      else if ($this->program == '3') {
        return 'Bachelor of Computer Science';
      }
  }


Mutator  

A function that set our attribute to the value of . For java, we called settor. Below shows the example of a function that set the attribute program name into the value we wan.
 public function setProgramNameAttribute($value)
  {
      $this->program = strtolower($value)
  }

No comments:

Post a Comment