Install the package via Composer:
composer require vendor/package-name
Publish the configuration (if applicable) and run migrations:
php artisan vendor:package publish --provider="Vendor\Package\ServiceProvider"
php artisan migrate
The package now provides two key methods for working with slug-based content retrieval:
getContentFromSlug($slug) – Fetch a content collection by slug.getRelatedContentFromContent($slug, $max) – Retrieve related content for a given slug (limited to $max results).First Use Case:
// Fetch a content collection by slug
$content = app('content-service')->getContentFromSlug('example-slug');
// Get related content (e.g., top 5)
$related = app('content-service')->getRelatedContentFromContent('example-slug', 5);
Check the config/package.php for default settings (e.g., related content logic, caching behavior).
Slug-Based Retrieval
Use getContentFromSlug() for direct content access (e.g., in route controllers or Blade templates):
public function show(ContentService $service, $slug) {
$content = $service->getContentFromSlug($slug);
return view('content.show', compact('content'));
}
Related Content Attach related content to views or APIs:
$related = $service->getRelatedContentFromContent($slug, 3);
return response()->json(['content' => $content, 'related' => $related]);
Caching Leverage Laravel’s cache (e.g., Redis) for slug-based queries:
$content = Cache::remember("content_{$slug}", now()->addHours(1), function() use ($slug) {
return $service->getContentFromSlug($slug);
});
Service Provider Binding
Bind the service in AppServiceProvider for cleaner dependency injection:
$this->app->bind('content-service', function($app) {
return new \Vendor\Package\Services\ContentService();
});
Route Model Binding Extend Laravel’s binding to resolve slugs:
Route::bind('content', function($slug) {
return app('content-service')->getContentFromSlug($slug);
});
Eloquent Relationships If using Eloquent, alias methods for consistency:
class Content extends Model {
public function related() {
return $this->hasManyThrough(RelatedModel::class, RelatedPivot::class)
->limit(app('content-service')->getRelatedLimit());
}
}
Slug Uniqueness
Ensure slugs are unique in your database to avoid conflicts in getContentFromSlug(). Add a database unique constraint if missing.
Performance with getRelatedContentFromContent()
The max parameter defaults to null (unlimited). Set a reasonable limit (e.g., 5) to avoid N+1 queries or excessive data loading.
Caching Invalidation Manually clear related content caches when content is updated:
Cache::forget("related_{$slug}");
Missing Slugs
Verify slugs are generated correctly (e.g., via Str::slug()) and stored in the database. Use:
dd(app('content-service')->getContentFromSlug('nonexistent-slug')); // Returns null or empty collection.
Related Content Logic Override the default related content algorithm by extending the service:
class CustomContentService extends \Vendor\Package\Services\ContentService {
public function getRelatedContentFromContent($slug, $max) {
// Custom logic (e.g., tag-based or distance-based)
}
}
Custom Related Content Logic
Override the getRelatedContentFromContent() method in a child class or use a strategy pattern:
$service->setRelatedContentStrategy(new TagBasedStrategy());
Slug Normalization Extend slug handling (e.g., locale support) by modifying the underlying query:
$service->setSlugNormalizer(function($slug) {
return Str::lower(trans($slug));
});
Event Hooks Listen for content updates to refresh caches or triggers:
Content::updated(function($content) {
Cache::forget("content_{$content->slug}");
});
How can I help you explore Laravel packages today?