So I have this need to check if a customer needs to be called. Customers has to be called at intervals depending on a value days_between_calls
in a BelongsTo
model called SubscriberType
. I got it to work but I don't like it, maybe there is a cleaner way.
So I have a model Subscription
with relations :
public function subscriberType()
{
return $this->belongsTo(SubscriberType::class);
}
public function calls()
{
return $this->hasMany(Call::class);
}
and a (very simplified) scope :
public function scopeNeedsCall(Builder $query) {
$query->join('subscriber_types', 'subscriber_types.id', '=', 'subscriptions.subscriber_type_id')
->whereDoesntHave('calls', function(Builder $query) {
$query->whereRaw('calls.created_at > DATE_SUB(NOW(), INTERVAL days_between_calls DAY)');
});
}
Is there any cleaner way to use this days_between_calls
field's value without manually joining its table and without writing raw sql?
Thanks ahead.
CodePudding user response:
So it looks like there is not much that can be improved, and I do need a rawsql part here. I improved it a little anyway using https://laravel.com/docs/9.x/eloquent-relationships#has-one-of-many but that's about it.