From 81e665b1bf402127808ae164c88a77ed94b55a5c Mon Sep 17 00:00:00 2001 From: JakeOcean <22035949+JakeOcean@users.noreply.github.com> Date: Tue, 4 Feb 2025 22:36:45 -0500 Subject: [PATCH 1/2] Update HasRecommendation.php Add support for additional operators --- src/HasRecommendation.php | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/HasRecommendation.php b/src/HasRecommendation.php index 7f50fb3..945234f 100644 --- a/src/HasRecommendation.php +++ b/src/HasRecommendation.php @@ -79,7 +79,25 @@ public static function getData($config) if (is_array($config['recommendation_data_table_filter'])) { foreach ($config['recommendation_data_table_filter'] as $field => $value) { - $data = $data->where($field, $value); + if (is_array($value) && count($value) >= 1) { + $operator = strtoupper((string)$value[0]); + $filterValue = $value[1] ?? null; + if ($operator === 'NULL') { + $data = $data->whereNull($field); + } elseif ($operator === 'NOT NULL') { + $data = $data->whereNotNull($field); + } elseif ($operator === 'IN') { + $data = $data->whereIn($field, $filterValue); + } elseif ($operator === 'NOT IN') { + $data = $data->whereNotIn($field, $filterValue); + } elseif (in_array($operator, ['=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE'])) { + $data = $data->where($field, $operator, $filterValue); + } else { + $data = $data->where($field, $value); + } + } else { + $data = $data->where($field, $value); + } } } From 76a332365e1f5fdddd336f32aab98dbeb2a48da7 Mon Sep 17 00:00:00 2001 From: JakeOcean <22035949+JakeOcean@users.noreply.github.com> Date: Tue, 4 Feb 2025 22:41:43 -0500 Subject: [PATCH 2/2] Update README.md Update readme with info about new Operators --- README.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 224d7d2..3229693 100644 --- a/README.md +++ b/README.md @@ -69,7 +69,7 @@ Add `HasRecommendation` trait and `InteractWithRecommendation` interface to the * **recommendation_algorithm**: The choice of method for generating recommendations. The choices are `db_relation` and `similarity` for now. Some of the other keys are mandatory according to the choice. * **recommendation_data_table**: The name of the data table which is mandatory when `db_relation` algorithm is choosen. -* **recommendation_data_table_filter**: The array of the filter values to be used in the query for fetching data from data table which is optional and can be used when `db_relation` algorithm is choosen. The array will contain fields and values as key-value pairs. +* **recommendation_data_table_filter**: The array of the filter values to be used in the query for fetching data from data table which is optional and can be used when `db_relation` algorithm is choosen. The array will contain fields and values as key-value pairs. Pass an array like `'product_id' => ['<', '500']` for more advanced operators * **recommendation_data_field**: The name of the data field which is mandatory when `db_relation` algorithm is choosen. * **recommendation_data_field_type**: The model class of the data field which is optional and can be used when `db_relation` algorithm is choosen. * **recommendation_group_field**: The name of the group field which is mandatory when `db_relation` algorithm is choosen. @@ -107,7 +107,10 @@ class ModelName extends Model implements InteractWithRecommendation 'recommendation_algorithm' => 'db_relation', 'recommendation_data_table' => 'recommendation_data_table', 'recommendation_data_table_filter' => [ - 'field' => 'value' + 'field' => 'value', + // array syntax supports: '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE' + 'product_id' => ["NOT NULL"], + 'cost' => ["<", "500"], ], 'recommendation_data_field' => 'recommendation_data_field', 'recommendation_data_field_type' => 'recommendation_data_field_type',