Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 需要为全局查询范围编写remove()的帮助吗_Php_Laravel_Laravel 5 - Fatal编程技术网

Php 需要为全局查询范围编写remove()的帮助吗

Php 需要为全局查询范围编写remove()的帮助吗,php,laravel,laravel-5,Php,Laravel,Laravel 5,这是我的范围,apply()方法工作得非常好 使用Illumb\Database\Elount\ScopeInterface; 使用Illumb\Database\Elount\Builder; 使用Illumb\Database\Elount\Model; 使用碳\碳; 类JobStatusScope实现ScopeInterface { /** *对查询应用范围。 * *@param\illumb\Database\elount\Builder$Builder *@param\illumb\D

这是我的范围,
apply()
方法工作得非常好

使用Illumb\Database\Elount\ScopeInterface;
使用Illumb\Database\Elount\Builder;
使用Illumb\Database\Elount\Model;
使用碳\碳;
类JobStatusScope实现ScopeInterface
{
/**
*对查询应用范围。
*
*@param\illumb\Database\elount\Builder$Builder
*@param\illumb\Database\elount\Model$Model
*@返回无效
*/
公共功能应用(Builder$Builder,Model$Model)
{
$builder
->其中('active',true)
->其中有('user',函数($q){
$q->where('账户类型','公司')
->whereNested(函数($r){
$r->where('stripe\u active',true)
->或wherenotnull('订阅在'结束')
->其中('subscription_结束于','>',Carbon::now())
->或wherenotnull('审判在')
->其中('trial_End_at','>',Carbon::today());
});
});
}
/**
*从查询中删除范围。
*
*@param\illumb\Database\elount\Builder$Builder
*@param\illumb\Database\elount\Model$Model
*@返回无效
*/
公共功能删除(生成器$Builder,模型$Model)
{
$query=$builder->getQuery();
//dd($query->where);
$columns=['stripe_active'、'subscription_ends_at'、'active'、'trial_ends_at'];
foreach((数组)$query->where as$key=>$where){
if(在_数组中($where['column',$columns)){
取消设置($query->where[$key]);
$query->where=array\u值($query->where);
}
}
}
}
我在
remove()
方法中尝试的方法无效

我把它放在一个trait文件中,并附在我的模型上

使用App\Cable\Traits\Scopes\JobStatusScope;
特质工作状态特质{
公共静态函数bootJobStatusTrait()
{
静态::addGlobalScope(新作业状态范围);
}
/**
*获取未应用范围的查询生成器。
*
*@return\illumb\Database\elount\Builder
*/
带all()的公共静态函数
{
返回时带有(新静态)->newQueryWithoutScope(新JobStatusScope);
}
}
当我尝试运行
MyModel::withAll()->get()
时,它应该返回所有没有
apply()
约束的记录,但它没有

当我在
remove()
方法中
dd($query->where)
并调用
->withAll()
时,我得到了这个结果

数组:3[▼
0=>数组:3[▼
“类型”=>“空”
“列”=>“作业。已在处删除”
“布尔值”=>“和”
]
1=>数组:5[▼
“类型”=>“基本”
“列”=>“活动”
“运算符”=>“=”
“值”=>true
“布尔值”=>“和”
]
2=>数组:5[▼
“类型”=>“基本”
“列”=>表达式{#478▼
#值:“(从`users`中选择count(*),其中`jobs`.`user\u id`=`users`.`id`和`account\u type`=?和(`stripe\u active`=?或`subscription\u ends\u at`不为空,`subscription\u ends\u at`>,或`trial\u ends\u at`不为空,`trial\u ends\u ends\u at`>)和`users`.`deleted\u at`为空)
}
“运算符”=>“>=”
“value”=>表达式{#476▶}
“布尔值”=>“和”
]
]
我似乎无法理解这一点。拉威尔的大部分作品都很容易理解,而且非常“有说服力”,但这让我无法理解

任何帮助都将不胜感激。谢谢!

通过使用子查询获取相关记录的计数,
whereHas()
限制起作用

dd()
输出中可以看到,目标
where
子句的
不仅仅是一个普通字符串(如“active”),而是一个包含子查询的表达式对象

您需要编写一些逻辑来确定此表达式是否与
whereHas()
生成的子查询匹配,以便在匹配时将其删除

一个简单的示例如下(注意:未检查或测试语法):

public函数删除(Builder$Builder,Model$Model)
{
$query=$builder->getQuery();
//您的普通列名
$columns=['active'];
//用户关系子查询中存在的列
$userColumns=['stripe\u active'、'subscription\u ends\u at'、'trial\u ends\u at'];
foreach((数组)$query->where as$key=>$where){
//首先检查列是否真的是表达式
//如果它不是表达式,请对字符串执行常规检查
if($where['column']instanceof\illumb\Database\Query\Expression){
//获取表达式的sql值
$sql=$where['column']->getValue();
//查看是否所有用户列都在sql语句中
$count=0;
foreach($userColumns作为$name){
//如果找到用户列,则递增计数
$count+=(stripos($sql,$name)!==false)?1:0;
}
//如果找到了所有用户列,则假定这是where并将其删除
如果($count==count($userColumns)){
取消设置($query->where[$key]);
$query->where=array\u值($query->where);
}
}elseif(在数组中($where['column',$columns)){
取消设置($query->where[$key]);
$query->where=array\u值($query->where);
}
}
}

您可能想提出比检查所有列名是否都在SQL中更严格的方法,但这应该可以让您开始了。

我提出了一个类似的解决方案,我将奖励您,因为这是我的想法。谢谢-