我有一个有效值的静态列表,例如$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);
}