In laravel how to pass extra data to mutators and accessors

Judging from the Laravel source code, no – it's not possible to pass an extra argument to this magic accessor method.

The easiest solution is just to add another, extra method in your class that does accept any parameters you wish – and you can use that method instead of magic property.

Eg. simply rename your getCommentsAttribute() to getComments() and fire ->getComments() instead of ->comments in your view, and you are good to go.


I just set a public property on the model. At the accessing point, I update that property to my desired value. Then, in the attribute method, I read the desired arguments from that property. So, putting all of that together,

// Model.php

public $arg1= true;

public function getAmazingAttribute () {

   if ($this->arg1 === false)
      $this->relation()->where('col', 5);

   else $this->relation()->where('col', 15);
}

// ModelController.php
$instance->arg1 = false;

$instance->append('amazing');