Laravel-5.6“喜欢”在选择(Laravel-5.6 'LIKE' in w

2019-10-28 10:45发布

我用这个查询,我得到一个错误:

 $description = $request->get('description');
        if (!empty($description)){
        $description_query = Transcationhistorique::where(['sender_id' => $user_id, "%$description%", 'LIKE','description'])
            ->orWhere('receiver_id', $user_id)->get();
        }else{
            $description_query  =  "" ;
        }

这是我得到的错误:

“SQLSTATE [42S22]:在'where子句'(SQL 1054未知列'0'::柱未找到SELECT * FROM transcation_historique其中( sender_id = 32和0 =%salaire%和1 = LIKE和2 =描述)或receiver_id = 32)”

这真是我想要的运行:

select * from `transcation_historique` where (`sender_id` = 32 and `description` = %salaire%) or `receiver_id` = 32)

Answer 1:

尝试这个,

$description_query = Transcationhistorique::where(
                         function($query) use ($user_id, $description){
                             return $query->where('sender_id', $user_id)
                                          ->where('description', 'like', '%' . $description .'%');

                         }
                     )
                     ->orWhere('receiver_id', $user_id)
                     ->get();


Answer 2:

好像你在那里查询不正确的结构。 你应该用以下结构,如果你想使用“=”其他运营商来源

$query->where([
    ['column_1', '=', 'value_1'],
    ['column_2', '<>', 'value_2'],
    [COLUMN, OPERATOR, VALUE],
    ...
])

我的建议是:

 $description = $request->get('description');
 if (!empty($description)){
     $description_query = Transcationhistorique::where([
            ['sender_id', '=', $user_id],
            ['description', 'LIKE', "%{$description}%"]
     ])
     ->orWhere('receiver_id', $user_id)->get();
 }else{
     $description_query  =  "" ;
 }



Answer 3:

试试这个:

Transcationhistorique::where('receiver_id', '=', $user_id)
        ->orWhere(function ($query) use ($user_id, $description) {
            $query->where('sender_id', '=', $user_id)->where('description', 'LIKE', "%".$description."%");
        })->get();


Answer 4:

您可以使用多个where的方法and 。 你可以试试下面的代码?

    $description = $request->get('description');
    if (!empty($description)){
        $description_query = Transcationhistorique::where('sender_id', $user_id)
        ->where("description", 'LIKE','%' . $description . '%')
        ->orWhere('receiver_id', $user_id)->get();
    }else{
        $description_query  =  "" ;
    }


文章来源: Laravel-5.6 'LIKE' in where select