PHP code example of jeffersongoncalves / laravel-knowledge-base
1. Go to this page and download the library: Download jeffersongoncalves/laravel-knowledge-base 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/ */
jeffersongoncalves / laravel-knowledge-base example snippets
'table_prefix' => 'kb_',
'models' => [
'article' => \App\Models\Article::class,
'category' => \App\Models\Category::class,
'article_version' => \App\Models\ArticleVersion::class,
'article_feedback' => \App\Models\ArticleFeedback::class,
'article_relation' => \App\Models\ArticleRelation::class,
],
'default_visibility' => 'public', // default visibility for new articles
'versioning_enabled' => true, // track article version history
'feedback_enabled' => true, // allow user feedback
'track_views' => true, // track article view counts
'search_engine' => 'database', // search engine type
'search_results_limit' => 20, // max results per search
use JeffersonGoncalves\KnowledgeBase\Services\KnowledgeBaseService;
$service = app(KnowledgeBaseService::class);
// Auto-generates slug from name
$category = $service->createCategory([
'name' => 'Getting Started',
]);
// With custom slug and parent
$child = $service->createCategory([
'name' => 'Installation',
'slug' => 'installation-guide',
'parent_id' => $category->id,
'description' => 'How to install the application',
'icon' => 'heroicon-o-wrench',
'sort_order' => 1,
]);
// Update
$service->updateCategory($category, ['name' => 'Quick Start']);
// Delete (soft delete)
$service->deleteCategory($category);
$article = $service->createArticle([
'category_id' => $category->id,
'title' => 'How to Install',
'slug' => 'how-to-install',
'content' => 'Step 1: Run composer App step by step.',
'seo_keywords' => 'install, setup, getting started',
'metadata' => ['difficulty' => 'beginner'],
], $author); // $author is any Eloquent Model (User, Admin, etc.)
$updated = $service->updateArticle($article, [
'title' => 'How to Install (Updated)',
'content' => 'Updated installation steps...',
], $editor, 'Fixed outdated instructions');
// Publish — sets status to Published, sets published_at, dispatches ArticlePublished
$service->publishArticle($article);
// Archive — sets status to Archived
$service->archiveArticle($article);
// Delete — soft deletes
$service->deleteArticle($article);
// Authenticated helpful feedback
$service->addFeedback($article, true, $user, 'Very clear, thank you!', '127.0.0.1');
// Anonymous not-helpful feedback
$service->addFeedback($article, false, null, 'Needs more examples');
// Anonymous without comment
$service->addFeedback($article, true);
// Basic search (only published articles)
$results = $service->search('installation');
// With filters
$results = $service->search('installation', [
'category_id' => 1,
'visibility' => 'public',
'limit' => 10,
]);
use JeffersonGoncalves\KnowledgeBase\Support\ModelResolver;
$articleClass = ModelResolver::article();
$categoryClass = ModelResolver::category();
// Article scopes
$published = $articleClass::published()->get();
$drafts = $articleClass::draft()->get();
$archived = $articleClass::archived()->get();
$internal = $articleClass::byVisibility(ArticleVisibility::Internal)->get();
// Category scopes
$active = $categoryClass::active()->get();
$roots = $categoryClass::root()->get();
$ordered = $categoryClass::ordered()->get();
$tree = $categoryClass::active()->root()->ordered()->get();
// Relationships
$article->category;
$article->author;
$article->versions;
$article->feedback;
$article->relatedArticles;
$category->parent;
$category->children;
$category->articles;
// View tracking
$article->incrementViewCount();
namespace App\Models;
use JeffersonGoncalves\KnowledgeBase\Models\Article as BaseArticle;
use JeffersonGoncalves\KnowledgeBase\Models\Contracts\ArticleContract;
class Article extends BaseArticle implements ArticleContract
{
// Add custom relationships, scopes, methods...
public function comments()
{
return $this->hasMany(Comment::class);
}
}
// config/knowledge-base.php
'models' => [
'article' => \App\Models\Article::class,
// ...
],
use JeffersonGoncalves\KnowledgeBase\Events\ArticlePublished;
class SendArticleNotification
{
public function handle(ArticlePublished $event): void
{
// Notify subscribers about the new article
$article = $event->article;
}
}
use JeffersonGoncalves\KnowledgeBase\Enums\ArticleStatus;
use JeffersonGoncalves\KnowledgeBase\Enums\ArticleVisibility;
$status = ArticleStatus::Published;
$status->label(); // 'Published' or 'Publicado'
$visibility = ArticleVisibility::Internal;
$visibility->label(); // 'Internal' or 'Interno'
bash
php artisan vendor:publish --tag="knowledge-base-migrations"
php artisan migrate
bash
php artisan vendor:publish --tag="knowledge-base-config"