skip to Main Content

How could I use another class in my vendor packages?
I want to implements "Searchable" & "Viewable" and add my Seo morphOne relation to a package like RinvexCategorizable, how can I do this without adding codes in to this package {from outside of vendor package}??

2

Answers


  1. You can try to extend class from package that you need and add relation there.
    Like that:

    namespace AppModels;
    
    use AppModelsProduct;
    use SpatieSearchableSearchable;
    use CyrildeWitEloquentViewableContractsViewable;
    use RinvexCategoriesModelsCategory as RinvexCategory;
    
    class Category extends RinvexCategory implements Searchable, Viewable
    {
        // Add relactions, override RinvexCategory methods or anything that you need :)
        public function products()
        {
            return $this->hasMany(Product::class);
        }
    }
    

    And use AppModelsCategory in you other code.

    Login or Signup to reply.
  2. You can create a model in your app and extends the package(vendor) model. Then define anything you want, add new traits or you can override methods.
    You can use your model instead of the one provided by the package.

    for example:

    use RinvexCategoriesModelsCategory;
    
    class MyModel extends Category {
    
      use MyExampleTraits, SearchableTraits;
    
      // You can add a new column to the table by publishing package migrations
     // or you can create a new migration to add your new columns
     
      public function seo() {
          return $this->morphOne(Seo::class, 'seoable');
      }
    }
    

    Note that the above code is just for a demonstration to give you hint. I hope you find it helpful.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search