PHP code example of boreistudio / filament-recommendations

1. Go to this page and download the library: Download boreistudio/filament-recommendations 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/ */

    

boreistudio / filament-recommendations example snippets


use BoreiStudio\FilamentRecommendations\Traits\HasRecommendations;

class Product extends Model
{
    use HasRecommendations;
}

use BoreiStudio\FilamentRecommendations\Resources\Concerns\HasRecommendationsWidget;

class ViewProduct extends ViewRecord
{
    use HasRecommendationsWidget; // shows the RecommendationsWidget on the View page
}

$product->recommend();                 // Collection of recommended models
$product->recordInteraction('viewed', $otherProduct);

use BoreiStudio\FilamentRecommendations\Contracts\TenantResolver;

class YourTenantResolver implements TenantResolver
{
    public function resolveTenantId(): int|string|null
    {
        return Tenant::current()?->id; // whatever your tenancy setup uses
    }
}

use BoreiStudio\FilamentRecommendations\Strategies\CoOccurrenceStrategy;

$strategy = new CoOccurrenceStrategy;
$results = $strategy->compute($product, 10); // [[recommended_type, recommended_id, score], ...]

// config/recommendations.php
'strategies' => [
    'weights' => [
        Product::class => ['category' => 2, 'price' => 0.5],
    ],
],

class Product extends Model implements Recommendable
{
    use HasRecommendations;

    public function getRecommendationFeatures(): array
    {
        return [
            'category' => $this->category,
            'tags' => $this->tags,      // arrays are compared with Jaccard
            'price' => (float) $this->price,
        ];
    }
}

$product->recordInteraction('viewed', $otherProduct, $user); // subject, context, actor

// config/recommendations.php
'strategies' => [
    'models' => [
        Product::class => \BoreiStudio\FilamentRecommendations\Strategies\CollaborativeFilteringStrategy::class,
    ],
],

use BoreiStudio\FilamentRecommendations\Resources\Concerns\HasRecommendationsWidget;

class ViewProduct extends ViewRecord
{
    use HasRecommendationsWidget;
}

// App\Providers\Filament\TenantPanelProvider.php
->plugins([
    \BoreiStudio\FilamentRecommendations\Filament\RecommendationsPlugin::make(),
])

// App\Providers\Filament\SuperAdminPanelProvider.php
->plugins([
    \BoreiStudio\FilamentRecommendations\Filament\RecommendationsPlugin::make()
        ->withoutSettingsPage()
        ->withSuperAdminPage(),
])

use BoreiStudio\FilamentRecommendations\Contracts\TenantProvider;

class YourTenantProvider implements TenantProvider
{
    public function allTenantIds(): array
    {
        return Tenant::query()->pluck('id')->all();
    }
}

// app/Console/Kernel.php
protected function schedule(Schedule $schedule): void
{
    $schedule->command('recommendations:rebuild')->daily()->withoutOverlapping();
    $schedule->command('recommendations:prune-events')->daily();
}

$product->recordInteraction('viewed', $otherProduct); // queues a recalc job

$product->recommend(); // recommendations are already up to date

// config/recommendations.php
'events' => [
    'recalculate_on_interaction' => false,
],

// config/recommendations.php
'cache' => [
    'enabled' => true,
    'ttl' => 3600,
    'store' => env('RECOMMENDATIONS_CACHE_STORE'), // null = default cache store
],

use BoreiStudio\FilamentRecommendations\Support\RecommendationCache;

RecommendationCache::forget($product, $product->getRecommendationTenantId());

// app/Support/RecommendationsTenantResolver.php
use BoreiStudio\FilamentRecommendations\Contracts\TenantResolver;
use Spatie\Multitenancy\Models\Tenant;

class RecommendationsTenantResolver implements TenantResolver
{
    public function resolveTenantId(): int|string|null
    {
        return Tenant::current()?->getKey();
    }
}

// app/Support/RecommendationsTenantProvider.php
use BoreiStudio\FilamentRecommendations\Contracts\TenantProvider;

class RecommendationsTenantProvider implements TenantProvider
{
    public function allTenantIds(): array
    {
        return Tenant::query()->pluck('id')->all();
    }
}

// config/recommendations.php
'tenancy' => [
    'enabled' => true,
    'resolver' => RecommendationsTenantResolver::class,
    'tenant_provider' => RecommendationsTenantProvider::class,
],
bash
php artisan recommendations:install
bash
php artisan migrate
bash
php artisan recommendations:prune-events              # uses events.retention_days
php artisan recommendations:prune-events --days=30
php artisan recommendations:prune-events --tenant=42