Laravel-5.6'LIKE'在哪里选择

时间:2019-02-26 10:56:14

标签: php mysql laravel laravel-5 laravel-5.6

我使用此查询,但出现错误:

 $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]:找不到列:1054'where中的未知列'0'   子句”(SQL:从transcation_historique中选择*,其中   (sender_id = 32,0 =%salaire%,1 =相似,2 =   说明)或receiver_id = 32)“

这就是我真正想要运行的:

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

4 个答案:

答案 0 :(得分:3)

尝试一下,

$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();

答案 1 :(得分:1)

似乎您的where查询结构不正确。 如果要使用除“ =”之外的其他运算符,则应使用以下结构 Source

    [DllImport("NativeCore.dll", CallingConvention = CallingConvention.Cdecl)]
    [return: MarshalAs(UnmanagedType.LPWStr)]
    public static extern string GetNodeName(IntPtr ptr);

}

我的建议是:

[DllImport( "my.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode )]
private static extern void GetMyString(StringBuffer str, int len);
public string GetMyStringMarshal()
{
    StringBuffer buffer = new StringBuffer(255);
    GetMyString(buffer, buffer.Capacity);
    return buffer.ToString();
}

答案 2 :(得分:1)

尝试这个:

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

答案 3 :(得分:0)

您可以将多个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  =  "" ;
    }