PHP code example of agenciafmd / admix-categories
1. Go to this page and download the library: Download agenciafmd/admix-categories library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
agenciafmd / admix-categories example snippets
return [
'name' => 'Categories',
'icon' => 'category',
'sort' => 100,
'categories' => [
[
'model' => \Agenciafmd\Articles\Models\Article::class,
'name' => 'Artigos',
'slug' => 'articles',
'types' => [
[
'name' => 'Categorias',
'slug' => 'categories',
],
// adicione quantos tipos forem necessários
],
],
// adicione quantas models forem necessários
],
];
protected function registerConfigs()
{
$this->mergeConfigFrom(__DIR__ . '/../config/admix-categories.php', 'admix-categories');
}
[
'name' => config('admix-articles.name') . ' » ' . config('admix-categories.articles-categories.name'),
'policy' => '\Agenciafmd\Articles\Policies\CategoryPolicy',
'abilities' => [
[
'name' => 'visualizar',
'method' => 'view',
],
[
'name' => 'criar',
'method' => 'create',
],
[
'name' => 'atualizar',
'method' => 'update',
],
[
'name' => 'deletar',
'method' => 'delete',
],
[
'name' => 'restaurar',
'method' => 'restore',
],
],
'sort' => 10
],
namespace Agenciafmd\Articles\Policies;
use Agenciafmd\Admix\Policies\AdmixPolicy;
class CategoryPolicy extends AdmixPolicy
{
//
}
use Agenciafmd\Articles\Policies\CategoryPolicy;
use Agenciafmd\Articles\Models\Category;
...
protected $policies = [
...
Category::class => CategoryPolicy::class,
];
...
namespace Agenciafmd\Articles\Http\Components\Aside;
use Illuminate\Contracts\View\View;
use Illuminate\View\Component;
class Article extends Component
{
public function __construct(
public string $icon = '',
public string $label = '',
public bool $active = false,
public bool $visible = false,
public array $children = [],
) {}
public function render(): View
{
$model = 'articles';
$types = collect(config('admix-categories.categories'))
->where('slug', $model)->first()['types'];
$children = collect($types)->map(function ($item) use ($model) {
return [
'label' => __($item['name']),
'url' => route('admix.categories.index', [
'categoryModel' => $model,
'categoryType' => $item['slug'],
]),
'active' => request()?->is("*{$model}/{$item['slug']}*"),
'visible' => true,
];
})->toArray();
$this->icon = __(config('admix-articles.icon'));
$this->label = __(config('admix-articles.name'));
$this->active = request()?->currentRouteNameStartsWith(['admix.articles']) || (request()->categoryModel === $model);
$this->visible = true;
$this->children = [
...$children,
[
'label' => __(config('admix-articles.name')),
'url' => route('admix.articles.index'),
'active' => request()?->currentRouteNameStartsWith('admix.articles'),
'visible' => true,
],
];
return view('admix::components.aside.dropdown');
}
}
public function builder(): Builder
{
return $this->model::query()
->with(['categories'])
->when($this->isTrash, function (Builder $builder) {
$builder->onlyTrashed();
})
->when(!$this->hasSorts(), function (Builder $builder) {
$builder->sort();
});
}
public function filters(): array
{
$this->setAdditionalFilters([
SelectFilter::make(__('admix-articles::fields.category'), 'category')
->options(['' => __('-'), ...(new Article)->categoriesToSelect()])
->filter(static function (Builder $builder, string $value) {
$builder->whereHas('categories', function ($builder) use ($value) {
$builder->where($builder->qualifyColumn('model'), Article::class)
->where($builder->qualifyColumn('type'), 'categories')
->where($builder->qualifyColumn('id'), $value);
});
}),
MultiSelectFilter::make(__('admix-articles::fields.tags'), 'tags')
->options((new Article)->categoriesToSelect('tags'))
->filter(static function (Builder $builder, array $values) {
$builder->whereHas('categories', function ($builder) use ($values) {
$builder->where($builder->qualifyColumn('model'), Article::class)
->where($builder->qualifyColumn('type'), 'tags')
->whereIn($builder->qualifyColumn('id'), $values);
});
}),
]);
return parent::filters();
}
public function columns(): array
{
$this->setAdditionalColumns([
Column::make(__('admix-articles::fields.category'))
->label(
fn ($row, Column $column) => $row->categories
->where('type', 'categories')
->first()
?->name
)
->sortable()
->searchable(function (Builder $builder, $value) {
$builder->orWhereHas('categories', function ($builder) use ($value) {
$builder->where($builder->qualifyColumn('name'), 'like', '%' . $value . '%');
});
}),
Column::make(__('admix-articles::fields.tags'))
->label(
fn ($row, Column $column) => str($row->categories
->where('type', 'tags')
->pluck('name')?->implode(', '))->limit(40)
)
->sortable()
->searchable(function (Builder $builder, $value) {
$builder->orWhereHas('categories', function ($builder) use ($value) {
$builder->where($builder->qualifyColumn('name'), 'like', '%' . $value . '%');
});
}),
]);
return parent::columns();
}
#[Validate]
public int $category = 0;
#[Validate]
public array $tags = [];
public function setModel(Article $article): void
{
$this->article = $article;
if ($article->exists) {
//...
$this->category = $article
->loadCategory()
?->id ?? 0;
$this->tags = $article
->loadCategories('tags')
->pluck('id')
->toArray();
}
}
'category' => [
'ule::exists('categories', 'id')
->where(function (Builder $builder) {
$builder->where('model', Article::class)
->where('type', 'categories');
}),
],
'tags' => [
'
'category' => __('admix-articles::fields.category'),
'tags' => __('admix-articles::fields.tags'),
if (!$this->article->exists) {
$this->article->save();
}
$this->article->syncCategories([$this->category, ...$this->tags]);
return $this->article->save();
use Agenciafmd\Categories\Traits\WithCategories;
class Article extends Model
{
use WithCategories;
}
namespace Agenciafmd\Articles\Database\Seeders;
use Agenciafmd\Categories\Models\Category;
use Agenciafmd\Articles\Models\Article;
use Illuminate\Database\Seeder;
class ArticleCategoryTableSeeder extends Seeder
{
protected int $total = 100;
protected string $type = 'categories';
protected string $model = Article::class;
public function run(): void
{
Category::query()
->where('type', $this->type)
->where('model', $this->model)
->get()
->each
->delete();
collect(range(1, $this->total))
->each(function () {
Category::factory()
->create([
'type' => $this->type,
'model' => $this->model,
]);
});
}
}
collect(range(1, $this->total))
->each(function () {
Product::factory()
->withCategory()
->withTags(random_int(1, 3))
->create();
});
php artisan vendor:publish --tag="admix-categories:config"
public function withTags(int $total = 10, string $type = 'tags'): Factory
{
$categories = Category::query()
->where('type', $type)
->where('model', $this->model)
->inRandomOrder()
->pluck('id');
return $this->state(function (array $attributes) {
return [
//
];
})->afterMaking(function (Product $product) {
//
})->afterCreating(function (Product $product) use ($type, $categories, $total) {
$product->categories()
->where('model', $this->model)
->where('type', $type)
->sync($categories->random($total)->toArray(), false);
});
}
public function withCategory(string $type = 'categories'): Factory
{
return $this->withTags(1, $type);
}