Laravel检查字符串是否包含数据库中的单词

我有一个包含员工列表的表,我有一个可以包含员工姓名的字符串。

我创建了一个SQL查询,该查询显示字符串中已经存在的员工的姓名。但我无法将其转换为查询Laravel。

如何在Laravel中编写此请求?

SELECT title from employes WHERE "my string" like concat("%",title,"%")
xdherosgl 回答:Laravel检查字符串是否包含数据库中的单词

您的支票被颠倒了,这使编写变得不必要地复杂,您应该改为WHERE title LIKE '%my string%'

然后在Laravel中

Employee::where('title','like','%' . $myString . '%')->get();
,

您可以使用if (not str.empty()) { ... } 门面并在控制器中执行SQL查询,如下所示:

DB
,

尝试以下代码:

在laravel中,您应该使用以下查询

$string = "the employee Antoine is on leave,he must be replaced by Lionel";

$findstring = explode(' ',$string); 

$emp = Employee::where(function ($q) use ($findstring) {
       foreach ($findstring as $value) {
            $q->orWhere('title',"%{$value}%");
       }
       })->select('title')->get();

dd($emp);
,
$message = $request->input('message');
if($message) {
    $findstring = explode(' ',$message); 
    $excluded = DB::table('word_list')->where(function ($q) use ($findstring) {
        foreach ($findstring as $value) {
            $q->orWhere('words',"%{$value}");
        }
    })->select('words')->first();
}
if($excluded != null) {
    return response()->json(['success'=>false,'excluded'=>$excluded,'message'=>' This is an excluded message','status'=>'message not send']);
} else {
    broadcast(new NewMessage($message));
    return response()->json(['success'=>true,'message'=>$message,'status'=>'message sent']);
}
本文链接:https://www.f2er.com/2979456.html

大家都在问