You can probably do it with a lot of ways for example you can create an interface Recordable that a model which needs to be recorded implements then check if the repliable is recordable (using for example instanceof Recordable) and do your recording stuff
Nov 23, 2020
2
Level 13
determine if model is recordable
I have a trait that records the activity of a model
RecordsActivityTrait
One of the models is polymorphic and in some cases i don't want to record the activity for that particular case.
The model name is Reply and it has a repliable relationship
For example
if ( $reply->repliable_type == 'SomeName' ){
// activity should not be recorded
}
Now i'm wondering if i should create a method isRecordable for all models that i want to record the activity
public function isRecordable()
{
return true;
}
And for the Reply model it would be
public function isRecordable()
{
if ( $this->repliable_type == 'SomeName' ){
return false;
}
return true;
}
}
or should i create a class that determines if the model is recordable
For example
class DetermineRecordability
{
public function handle($model)
{
if ( class_basename($model) == 'Reply' && $model->repliable_type == 'SomeName'){
return false;
}
return true;
}
}
Which approach do you think is better ?
Please or to participate in this conversation.