May 18, 2018
0
Level 14
How to search concatenated fields with MySQL in production and sqlite when testing
Hello
I have a real hacky way of trying to get my tests to pass when I'm searching a concatenated field. For example, I store my users as first_name, last_name so when I come to search I would do something like this.
ExampleModel::where(DB::raw("concat(first_name, ' ', surname)"), 'LIKE', "%$term%")->get()
However, you get an error when testing (when you use sqlite) because it doesn't have the concat function. For this you would use something like this.
ExampleModel::where(DB::raw("first_name || ' ' || surname) , 'LIKE', "%$term%")->get()
Without having a check to see if your environment is testing, how would you go about searching for this that would work on both databases?
Please or to participate in this conversation.