Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Seo Laravel Package

tipoff/seo

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation

    composer require tipoff/seo
    

    Publish the config file:

    php artisan vendor:publish --provider="Tipoff\Seo\SeoServiceProvider" --tag="config"
    
  2. Configuration Edit config/seo.php to define:

    • keywords (array of tracked keywords)
    • models (ELOQUENT models to track keyword associations)
    • middleware (optional: Tipoff\Seo\Middleware\TrackKeywords)
  3. First Use Case Track a keyword for a model (e.g., Post):

    use Tipoff\Seo\Traits\HasKeywords;
    
    class Post extends Model
    {
        use HasKeywords;
    }
    
    // In a controller or model:
    $post = Post::find(1);
    $post->trackKeyword('laravel seo'); // Tracks the keyword for this post
    

Implementation Patterns

Core Workflows

  1. Tracking Keywords

    • Manual Tracking: Use trackKeyword() on any model with the HasKeywords trait.
      $model->trackKeyword('keyword phrase');
      
    • Automatic Tracking: Use middleware to auto-track keywords from request data (e.g., URL slugs or query params).
      // In config/seo.php:
      'middleware' => ['web'],
      
  2. Querying Models by Keywords

    • Fetch models associated with a keyword:
      $posts = Post::whereKeyword('laravel seo')->get();
      
    • Check if a model is associated with a keyword:
      if ($post->hasKeyword('laravel seo')) {
          // ...
      }
      
  3. Bulk Operations

    • Assign keywords to multiple models:
      $posts = Post::where('published', true)->get();
      $posts->each->trackKeyword('popular laravel topics');
      
  4. Analytics Integration

    • Use the Seo facade to fetch keyword analytics:
      use Tipoff\Seo\Facades\Seo;
      
      $stats = Seo::getKeywordStats('laravel seo');
      // Returns: ['count' => 42, 'models' => [...]]
      

Integration Tips

  1. Model Observers Track keywords automatically when models are created/updated:

    class PostObserver
    {
        public function saved(Post $post)
        {
            if ($post->isPublished()) {
                $post->trackKeyword('published content');
            }
        }
    }
    
  2. API Endpoints Expose keyword tracking via API:

    Route::post('/posts/{post}/keywords', function (Post $post) {
        $post->trackKeyword(request('keyword'));
        return response()->json(['success' => true]);
    });
    
  3. SEO Reports Generate reports in a controller:

    public function seoReport()
    {
        $keywords = Seo::getAllKeywords();
        $report = collect($keywords)->mapWithKeys(function ($keyword) {
            return [$keyword => Seo::getKeywordStats($keyword)];
        });
        return view('reports.seo', compact('report'));
    }
    
  4. Caching Cache keyword stats to improve performance:

    $stats = Cache::remember("seo:stats:{$keyword}", now()->addHours(1), function () use ($keyword) {
        return Seo::getKeywordStats($keyword);
    });
    

Gotchas and Tips

Pitfalls

  1. Case Sensitivity Keywords are stored in lowercase by default. Ensure consistency:

    // Avoid:
    $post->trackKeyword('Laravel SEO'); // Stored as 'laravel seo'
    $post->hasKeyword('laravel seo');   // Works
    $post->hasKeyword('Laravel SEO');   // Fails (case mismatch)
    

    Fix: Normalize keywords before tracking:

    $post->trackKeyword(strtolower(request('keyword')));
    
  2. Model Configuration Forgetting to add models to config/seo.php under the models key will cause HasKeywords to fail silently. Fix: Verify the config:

    'models' => [
        'App\Models\Post',
        'App\Models\Page',
    ],
    
  3. Middleware Conflicts The TrackKeywords middleware may interfere with other middleware (e.g., auth) if not ordered correctly. Fix: Register middleware in the correct group (e.g., web):

    // app/Http/Kernel.php
    protected $middlewareGroups = [
        'web' => [
            // ...
            \Tipoff\Seo\Middleware\TrackKeywords::class,
        ],
    ];
    
  4. Duplicate Keywords Tracking the same keyword multiple times on a model doesn’t create duplicates but may skew analytics. Fix: Check for existing associations:

    if (!$post->hasKeyword('laravel seo')) {
        $post->trackKeyword('laravel seo');
    }
    

Debugging Tips

  1. Log Keyword Tracking Enable logging in config/seo.php:

    'log_tracking' => env('SEO_LOG_TRACKING', false),
    

    Check storage/logs/laravel.log for tracking events.

  2. Database Inspection Keywords are stored in the seo_keywords table. Verify entries with:

    php artisan tinker
    >>> \Tipoff\Seo\Models\Keyword::all();
    
  3. Middleware Debugging Temporarily disable middleware to isolate issues:

    // In Kernel.php:
    protected $middleware = [
        // \Tipoff\Seo\Middleware\TrackKeywords::class, // Comment out
    ];
    

Extension Points

  1. Custom Keyword Sources Extend the Tipoff\Seo\Contracts\KeywordSource interface to pull keywords from external sources (e.g., Google Analytics):

    class AnalyticsKeywordSource implements KeywordSource
    {
        public function getKeywords()
        {
            return $this->fetchFromGoogleAnalytics();
        }
    }
    

    Register in config/seo.php:

    'sources' => [
        \App\Services\AnalyticsKeywordSource::class,
    ],
    
  2. Custom Storage Override the default pivot table by binding a custom model:

    class Post extends Model
    {
        use HasKeywords;
    
        protected $keywordModel = \App\Models\CustomKeyword::class;
    }
    
  3. Events Listen for keyword tracking events:

    event(new \Tipoff\Seo\Events\KeywordTracked($model, $keyword));
    

    Register listeners in EventServiceProvider:

    protected $listen = [
        \Tipoff\Seo\Events\KeywordTracked::class => [
            \App\Listeners\LogKeywordTrack::class,
        ],
    ];
    
  4. API Resources Extend the default JSON response for keywords:

    namespace App\Http\Resources;
    
    use Tipoff\Seo\Models\Keyword;
    use Illuminate\Http\Resources\Json\JsonResource;
    
    class KeywordResource extends JsonResource
    {
        public function toArray($request)
        {
            return [
                'name' => $this->name,
                'count' => $this->models->count(),
                'models' => $this->models->pluck('id'),
            ];
        }
    }
    

    Override the facade method:

    Seo::setResourceClass(KeywordResource::class);
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
cadot.eu/make
besmartand-pro/php-quality-config
sentix/ai-chatbot
codifyo/ts-generator-bundle
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky