This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

viral-vector
viral-vector

Is it possible to replace or extend (override) an existing method within a model class. I am looking to replace Blog plugin -> scopeIsPublished method to take account for an additional date check.

$model->addDynamicMethod('scopeIsPublished', function($query){
  return $query
   ->whereNotNull('published')
   ->where('published', true)
   ->whereNotNull('published_at')
   ->where('published_at', '<', Carbon::now())
   ->where(function($query){
       $query->whereNull('delete_at');
       $query->orWhere('delete_at', '>', Carbon::now());
   });
});

Last updated

viral-vector
alxy
alxy

Overriding is not possible (actual methods take precedence over dynamic ones). I recommend to create a scope with a different name.

viral-vector
viral-vector

@alxy. Thanks for the input. I cannot use another scope as that specific scope method is utilized by the base plugin/model in some methods. Meaning i want the base plugin/model to respect my new scope anytime it calls the original scope. To achieve this i would have to create a new model and override any methods that utilize 'scopeIsPublished'. This would become hard to maintain on updates.

alxy
alxy

You can also try to use model events, maybe something like onFetch. I'm not sure if you can determine if the scope is used and then add your conditions dynamically.

viral-vector
viral-vector

@alxy. No luck. I decided to add a new scope & call it as i need. I still have the issue of the base plugin not being aware to take account of the new scope. I could use a global scope, but it could affect the backend management?

1-6 of 6

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.