How to perform sql LIKE operation on Firebase in Laravel PHP #16

How to perform the like queries on Firebase because you know Firebase provide exact match "==" with case sensitive. So we will solve this issue in the chapter in detail. We will use the Laravel PHP SDK for the firebase for all the queries stuff.

Now lets consider a simple example, you have 'Student' list and you want to search by name. So the main use of any search is that, you insert one or two characters and the system should search the most matching one, but that not the case in Firebase. All Firebase developers are facing this issue, which is most basic necessity. So lets first understand, currently what is provided by Firebase. See the below example of Student List.



Now here you see, we have a Student named 'Tim Dane'. Now you have to search this student from whole list, so you start time 'Tim', so atleast it should be match one matching word. So do Firebase return has "Tim Dane" on search of "Tim". Let's check.



So, as per Firebase Console, we have option of "==", So we selected that and entered the keyword "Tim". You see the query above. So let's check what's the result?


So, it failed us. No result we got. So how Firebase works? Firebase works on exact match. So if you have added the whole name "Tim Dane", then it would have shown you the result. Even "tim dane" would not give you the result, because it is also case sensitive. We will have separate chapter on case sensitive query. But now what is use of writing whole name same to same and getting the result. Because it is making it totally useless on search of string. But don't worry the option hack you wouldn't find in Firebase Console, you can simply do it in your Laravel PHP code.

Let's begin the Laravel PHP code. You know how to fetch the Student list. Following is the reference of collection of Students

 $student = app('firebase.firestore')->database()->collection('Students');  

Now let us query like "%" (the sql like query) on this firebase collection

 $keyword = 'Tim';  
 $student = $student ->orderBy('name','asc')  
           ->startAt([$keyword])  
           ->endAt([$keyword."\uf8ff"]);  
Here we used Orderby field 'name' in ascending order and used the startAt and endAt function of Firebase query. So this query orders all data by name in ascending starting at and ending at keyword 'Tim'. Now the main hack is the appended string "\uf8ff". This unicode is most last private unicode. Read more on google for this. So by this we would get all data containing 'Tim' keywords. You can also get results for keyword 'Dane'.

So hope you have got par the very necessity functionality of every system that Firebase was not supporting. And now you can implement search in your all list pages.

Now this topic of search is very vast. See you got the result for 'Tim'. But do you get for smaller keyword 'tim'. No here still case sensitive issue is not solved. And what about you had two fields like firstname: 'Tim' and lastname 'Dane' instead of one field name: 'Tim Dane'. So this is another challenge for you. So lets note our two challenges 1) Case Sensitive 2) Multiple field search. Both topic will be covered in Separate chapter. So stay tuned with us. Please let us know your comments in comment section below. And please if you like our work and it helped you in any part of Upcoming project, please support us by sharing our link in every platform and with your friends. So it will keep us making new chapters and digging deep into new topics and helping you out from every situation. Thanks again for reading with us.

Post a Comment

3 Comments

  1. Replies
    1. Not optimal. We are trying to use cloud trigger function for these..will create chapter soon on this.

      Delete