我有一个有效值的静态列表,例如
$valid = ['one','two']
,types
),具有多个值(转换为数组)。
我可以检查它们是否相交:
array_intersect($model->types, $valid)
我可以创建一个仅包含相交模型的
scope
吗?
我认为你可以使用
orWhereJsonContains
功能,类似这样的东西:
public function scopeNameIt(Builder $query,$array): void //pass the array as a parameter to the scope
{
$query->where(function($query) use ($array)){
foreach($array as $element){
$query->orWhereJsonContains('types',element);
}
}
}
您可以在文档
中阅读相关内容根据文档,您也可以将数组传递给
whereJsonContains
函数
public function scopeNameIt(Builder $query,$array): void //pass the array as a parameter to the scope
{
$query->WhereJsonContains('types',$array);
}