Answer the question
In order to leave comments, you need to log in
Is it necessary to split the local scope into two depending on the number of values in the argument?
I would like to get opinions on how to write correctly / more correctly:
public function scopeByPartnerId($query, $partnerId)
{
if (is_countable($partnerId)) {
$query->whereIn('partner_id', $partnerId);
} else {
$query->where('partner_id', $partnerId);
}
}
public function scopeByPartnerId($query, $partnerId)
{
$query->where('partner_id', $partnerId);
}
public function scopeByPartnerIds(Builder $query, array $partnerIds)
{
$query->whereIn('partner_id', $partnerIds);
}
Answer the question
In order to leave comments, you need to log in
There is no “correct” in this matter and cannot be. It can be "more understandable for others" and "more supportive". For the second there is no difference, for the first I prefer this option:
public function scopeByPartnerId($query, int|array $partnerId)
{
if (is_countable($partnerId)) {
$query->whereIn('partner_id', $partnerId);
} else {
$query->where('partner_id', $partnerId);
}
}
You don't have to share anything. You need to leave one method of multiple selection.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question