i try make code for search and this is my code:
我尝试制作搜索代码,这是我的代码:
$services = Service::query()->with('plans')->latest();
if ($request->service_name) {
$services = $services->whereRaw("CONVERT(JSON_EXTRACT(name, '$.ar') using 'utf8') LIKE '%$request->service_name%' ")
->orWhereRaw("CONVERT(JSON_EXTRACT(name, '$.en') using 'utf8') LIKE '%$request->service_name%' ")
->orWhereRaw("CONVERT(JSON_EXTRACT(name, '$.he') using 'utf8') LIKE '%$request->service_name%' ");
}
if ($request->plan_name) {
$plan_name = $request->plan_name;
$services = $services->whereHas('plans', function ($q) use ($plan_name) {
$q->where('name->en','Like','%'.$plan_name.'%');
});
}
return $services->get();
but when I send plan_name in request the code filters by service name and not by plan name
但当我在请求中发送计划名称时,代码会按服务名称而不是计划名称进行筛选
i tried when is send the plan name in the request The code filter data by plan name but it does not work
我尝试过在请求中发送计划名称。代码按计划名称筛选数据,但不起作用
更多回答
Can you show us $request
? Is it possible that $request->service_name
comes back as true
, thus changing your $services
variable, and making it impossible to use again on your 2nd if
statement? In any case, I think it's not good to do $services = $services->...
. It's more likely to have unexpected results this way
你能给我们看看$request吗?$request->service_name是否可能返回true,从而更改您的$services变量,并使其无法在您的第二个if语句中再次使用?无论如何,我认为做$services=$services->.是不好的。。。。这样更有可能产生意想不到的结果
You should use not null with this, ex if ($request->service_name && !$request->plan_name) {
您应该使用not null,例如if($request->service_name!$request->plan_name){
@tola this it the request: service_name: plan_name: Te
@告诉它请求:服务名称:计划名称:Te
优秀答案推荐
if ($request->service_name && !$request->plan_name) { // When only service name provided
$services = $services->whereRaw("CONVERT(JSON_EXTRACT(name, '$.ar') using 'utf8') LIKE '%$request->service_name%' ")
->orWhereRaw("CONVERT(JSON_EXTRACT(name, '$.en') using 'utf8') LIKE '%$request->service_name%' ")
->orWhereRaw("CONVERT(JSON_EXTRACT(name, '$.he') using 'utf8') LIKE '%$request->service_name%' ");
}
if ($request->plan_name && !$request->service_name) { // When only plan name provided
$plan_name = $request->plan_name;
$services = $services->whereHas('plans', function ($q) use ($plan_name) {
$q->where('name->en','Like','%'.$plan_name.'%');
});
}
FYI: You can write this code better than this. This is just fixing your code only.
In several places you have used ->whereRaw("... LIKE '%$request->service_name%' ")
this must be replace with ->whereRaw("... LIKE '%{$request->service_name}%' ")
, notice the {...}
around {$request->...}
. What you were doing it converts the whole $request
to string but you only want a particular param like service_name
.
在您使用过->whereRaw(“…LIKE'%$request->service_name%'”)的几个地方,必须将其替换为->whereRaw(“..LIKE'%{$request->services_name}%'”。
By the way I have to point out this implementation is extremely bad and should be prohibited because you are exposing your system to SQL injection, meaning an attacker could easily read/destroy your database. You should see Laravel docs on how to setup query binding, it is pretty easy.
顺便说一句,我必须指出,这种实现非常糟糕,应该被禁止,因为您将系统暴露在SQL注入中,这意味着攻击者可以很容易地读取/销毁您的数据库。您应该看到关于如何设置查询绑定的Laravel文档,这很容易。
更多回答
我是一名优秀的程序员,十分优秀!