Reputation: 653
In my package I have come model with it own namespace
<?php
namespace Nosennij\LaravelCategoryMenuAndBreadcrumbs\models;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
public function scopeMain($query)
{
return $query->where('parent_id', 0);
}
public function subcategories(){
return $this->hasMany(Category::class, 'parent_id');
}
public function parent(){
return $this->belongsTo(Category::class, 'parent_id');
}
}
When I install package it will be good if I do not copy this model to app folder and do not change namespace to App. It will be better to extend this model from package imporing all parent methods. I try to do it next way
1) php artisan make:model Category
2) try to extend
namespace App;
use Illuminate\Database\Eloquent\Model;
use Nosennij\LaravelCategoryMenuAndBreadcrumbs\models\Category as ParentCategory;
class Category extends ParentCategory
{
public function __construct(array $attributes = [])
{
parent::__construct($attributes);
parent::boot();
}
//my own new methods or methods rewriting parent methods
}
How can I do it?
So in controller I want to use App\Category;
and want to have acces to scopeMain
, subcategories
, parent
methods from package model in vendor/nosennij/laravel-catmenubread/models/Category.php
Working variant- https://github.com/n-osennij/laravel-category
Upvotes: 0
Views: 1491
Reputation: 35170
In your package move your models
directory under src
and capitalize the M
e.g. src/Models/Category.php
.
Then change the namespace in Category
to be:
namespace Nosennij\LaravelCategoryMenuAndBreadcrumbs\Models;
Then in your Category
class in your app directory change it to be:
use Nosennij\LaravelCategoryMenuAndBreadcrumbs\Models\Category as ParentCategory;
class Category extends ParentCategory
Upvotes: 1