<?php
namespace App\Business\Recommendation;
use App\Models\Course;
use App\Models\Document;
use App\Models\Question;
trait HasViewsRecommendation
{
public static $recommendations = [
Document::class => 'suggested_documents',
Course::class => 'suggested_courses',
Question::class => 'suggested_questions'
];
public static $recommendationCount = 5;
private static function generateRelationAlgorithmArray(string $model, string $name): array
{
return
[$name => [
'recommendation_algorithm' => 'db_relation',
'recommendation_data_table' => 'user_views',
'recommendation_data_table_filter' => [],
'recommendation_data_field' => 'viewable_id',
'recommendation_data_field_type' => $model,
'recommendation_group_field' => 'user_id',
'recommendation_count' => self::$recommendationCount
]];
}
public static function getRecommendationAlgorithmArray(): array
{
$result = [];
foreach (self::$recommendations as $model => $name) {
$result = array_merge($result, self::generateRelationAlgorithmArray($model, $name));
}
return $result;
}
}
class User extends Authenticatable implements InteractWithRecommendation
{
use HasFactory,
HasRecommendation,
HasViewsRecommendation,
/**
* The user recommendation config.
* @return array
*/
public static function getRecommendationConfig(): array
{
return self::getRecommendationAlgorithmArray();
}
And i generated some recommendations using the laravel-model-recommendation table and here is some rows
Now based on my understanding .if the user logged in is of id 7 and i called the fuction php $user->getRecommendations('suggested_courses');
I should get the First 5 rows that are of type Courses .But the thing is that i always get an empty array .
To better undestand the situation I inspected the source code and I think it"s because of this query
The target_type is hardcoded to be the same as the source type.