phase 8 and 9
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class Segment extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $fillable = [
|
||||
'website_id',
|
||||
'name',
|
||||
'filters',
|
||||
];
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'filters' => 'array',
|
||||
];
|
||||
}
|
||||
|
||||
public function website(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Website::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the visitor IDs that match this segment's filters.
|
||||
*/
|
||||
public function matchingVisitorIds(): \Illuminate\Support\Collection
|
||||
{
|
||||
$query = Visitor::where('website_id', $this->website_id);
|
||||
|
||||
foreach ($this->filters as $filter) {
|
||||
$field = $filter['field'];
|
||||
$operator = $filter['operator'] ?? '=';
|
||||
$value = $filter['value'];
|
||||
|
||||
$allowedFields = ['country', 'region', 'city', 'browser', 'os', 'device_type', 'language', 'screen_resolution'];
|
||||
if (! in_array($field, $allowedFields)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
match ($operator) {
|
||||
'=' => $query->where($field, $value),
|
||||
'!=' => $query->where($field, '!=', $value),
|
||||
'contains' => $query->where($field, 'like', "%{$value}%"),
|
||||
'starts_with' => $query->where($field, 'like', "{$value}%"),
|
||||
default => null,
|
||||
};
|
||||
}
|
||||
|
||||
return $query->pluck('id');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user