sifangpay/app/Classc/SearchScopeTrait.php

133 lines
3.1 KiB
PHP

<?php
namespace App\Classc;
use App\Services\SearchServices;
trait SearchScopeTrait
{
public function scopeSearch($query,array $arr, $where = 2)
{
if(count($arr)<=0){
return false;
}
foreach ($arr as $k => $v) {
if ($where == 1) {
$where_value = 1;
} elseif($where == 3) {
if(empty(trim($v['value'])))
{
$where_value=0;
}else
{
$where_value=1;
}
if($v['value']=='0')
{
$where_value=1;
}
}else
{
if(empty(trim($v['value'])))
{
$where_value=0;
}else
{
$where_value=1;
}
}
if ($where_value==1) {
if($v['value']=='' && $v['value']!=0)
{
continue;
}
if ($v['type'] == '=') {
$query->where($k, $v['value']);
}
if ($v['type'] == 'in') {
$query->whereIn($k, $v['value']);
}
if ($v['type'] == '>') {
$query->where($k,'>' ,$v['value']);
}
if ($v['type'] == '>=') {
$query->where($k,'>=' ,$v['value']);
}
if ($v['type'] == '<>') {
$query->where($k,'<>' ,$v['value']);
}
if ($v['type'] == '<') {
$query->where($k,'<' ,$v['value']);
}
if ($v['type'] == '<=') {
$query->where($k,'<=' ,$v['value']);
}
if ($v['type'] == 'between') {
$query->whereBetween($k,$v['value']);
}
if ($v['type'] == 'raw') {
//dd($v['value'][0]);
$query->whereRaw($v['value'][0],[$v['value'][1]]);
}
if ($v['type'] == 'like') {
$query->whereRaw($k . ' like ?', ["%" . $v['value'] . "%"]);
}
if ($v['type'] == 'likesql') {
$query->whereRaw($v['value']);
}
}
}
return $query;
}
public static function getList($is_checked = 1, $field = '')
{
if (!$is_checked) {
if ($field == 'keyvalue') {
return self::pluck('name', 'id');
}
return self::get();
}
return self::where('is_checked', 1)->get();
}
public function getCheckNameAttribute()
{
$arr=['不通过','通过'];
return $arr[$this->is_checked];
}
public static function getSearchModel($model,$data,$type=''){
// print_r($data);
$search= new SearchServices($model,$data,$type);
return $search->getModel();
}
}