You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
44 lines
1.5 KiB
PHP
44 lines
1.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Helper\Macro;
|
|
|
|
use Hyperf\Database\Model\Relations\Relation;
|
|
|
|
class WhereHasMorphIn
|
|
{
|
|
public static function make()
|
|
{
|
|
return function ($relation, $types, $callback = null, $boolean = 'and') {
|
|
$relation = $this->getRelationWithoutConstraints($relation);
|
|
|
|
$types = (array) $types;
|
|
|
|
if ($types === ['*']) {
|
|
$types = $this->model->newModelQuery()->distinct()->pluck($relation->getMorphType())->all();
|
|
|
|
foreach ($types as &$type) {
|
|
$type = Relation::getMorphedModel($type) ?? $type;
|
|
}
|
|
}
|
|
|
|
return $this->where(function ($query) use ($relation, $callback, $types) {
|
|
foreach ($types as $type) {
|
|
$query->orWhere(function ($query) use ($relation, $callback, $type) {
|
|
$belongsTo = $this->getBelongsToRelation($relation, $type);
|
|
|
|
if ($callback) {
|
|
$callback = function ($query) use ($callback, $type) {
|
|
return $callback($query, $type);
|
|
};
|
|
}
|
|
|
|
$query->where($relation->getRelated()->getTable().'.'.$relation->getMorphType(), '=', (new $type)->getMorphClass())
|
|
->whereHasIn($belongsTo, $callback);
|
|
});
|
|
}
|
|
}, null, null, $boolean);
|
|
};
|
|
}
|
|
} |