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], ...]
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
use BoreiStudio\FilamentRecommendations\Resources\Concerns\HasRecommendationsWidget;
class ViewProduct extends ViewRecord
{
use HasRecommendationsWidget;
}
use BoreiStudio\FilamentRecommendations\Contracts\TenantProvider;
class YourTenantProvider implements TenantProvider
{
public function allTenantIds(): array
{
return Tenant::query()->pluck('id')->all();
}
}
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();
}
}