-
In the line above, only |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 11 replies
-
Hi. A trait is not aware where it is being used so your desire is not recommended. |
Beta Was this translation helpful? Give feedback.
-
you can of course bind your Closure to $this when passing it to the Attribute? Attribute::get(
Closure::bind(
function(){},
$this
)
); or Attribute::get(
(function(){})->bindTo($this)
); |
Beta Was this translation helpful? Give feedback.
-
Okay, I know the problem. This was my code I hoped would work: protected function nameFinal(): Attribute{
return Attribute::make(static function(mixed $value,array $attributes){
dump(get_defined_vars(),$this);
})->shouldCache();
} But this didn't work, so I made it like this: protected function nameFinal(): Attribute{
$record = $this;
return Attribute::make(static function(mixed $value,array $attributes) use($record){
dump(get_defined_vars(),$record);
})->shouldCache();
} However, it can be much simpler. First of all, The new code looks like this: protected function nameFinal(): Attribute{
return Attribute::make(function(mixed $value,array $attributes){
dump($this);
})->shouldCache();
} |
Beta Was this translation helpful? Give feedback.
-
@ben221199 Thanks for asking this question in an incomplete manner :) And I'm not joking. I learned new things from this thread. |
Beta Was this translation helpful? Give feedback.
Okay, I know the problem. This was my code I hoped would work:
But this didn't work, so I made it like this:
However, it can be much simpler. First of all,
get_defined_vars()
will not list$this
, so only$value
and$attributes
are visible. It isn't a good function to see if you have access to$this
. The real problem wa…