Laravel concat in query (where condition)

后端 未结 2 503
梦如初夏
梦如初夏 2021-01-01 14:17

I am pretty new to laravel (started today) and facing a problem building a simple query:

$query->orWhere(\"CONCAT(`nvp`, \' \', `vpv`)\", \'LIKE\', \"%$th         


        
相关标签:
2条回答
  • 2021-01-01 14:44

    Use orWhereRaw to execute a raw where query:

    $query->orWhereRaw("CONCAT(`nvp`, ' ', `vpv`) LIKE ?", ['%'.$this->searchNeedle.'%']);
    
    0 讨论(0)
  • 2021-01-01 14:46

    Laravel does some stuff behind the scenes like adding in the tick marks for you.

    Fortunately, it also offers a couple of tools to still get the job done for you...

    For this type of thing, DB::raw() usually works really well. Try something like this...

    $query->orWhere(DB::raw("CONCAT(`nvp`, ' ', `vpv`)"), 'LIKE', "%".$this->searchNeedle."%");
    
    0 讨论(0)
提交回复
热议问题