-1
I’m doing a query that performs where
based on the string filter
received in query params. My filter query params has this format:
filter: (status~contains~false~and~username~contains~'admin')
I need to make a search()
in that request.input('filter')
and search for the word contained after the ~contains~'palavra'
to do my query. I’m having a hard time doing this regex. (I’m still not sure if regex would be the way to solve the problem, but I believe it is the way.)
I would have something like:
if(request.input('filter').search('regex??'))
queryUsers.where('username', 'like', '%'+request.input('username')+'%')
@Edit:
what I’m trying to do is filter conditional, in a user search I can search for username and status, the frontend returns this query string and I need to check if there is username in the filter, if there is, I need these values to put in Where, currently my function is that way:
async index({request}) {
const { page, pageSize } = request.get();
const queryUsers = User
.query()
.with('user')
let filter = request.input('filter')
if(filter.search("username")){
let userName = filter.replace(/(?<=')(.*?)(?=')/g, '')
console.log(userName)
queryUsers.where('username', 'like', '%'+userName+'%')
}
const users = await queryUsers.paginate(page, pageSize)
return users
}
It was not very clear your question, you need which part of the string?
– LeAndrade
@Leandrade edited the question, see if it got better to understand
– veroneseComS
But by the example he gave status~contains~false~and~username~contains~'admin', you would need to take the word
'admin'
?– LeAndrade
that, correct. :)
– veroneseComS
But your regex is already doing it.
– LeAndrade
in fact I need the inverse of what I did, because in this way I’m replacing the value that is between ' ' by empty, so it’s everything but what I need. And when it’s not a string and a boolean, which is the status case, this logic won’t work
– veroneseComS
But status tbm will be able to have quotes?
– LeAndrade
no, so this logic doesn’t work for me, and also if there was more than one string I think it would ruin :/
– veroneseComS