title: How to Match on Joined Collections Using Laravel and MongoDB?
body: <p>I have two collections "bookings" and "users". In "bookings" collection I have a field <code>"user"</code>. It is related to <code>"users._id"</code>. </p>
  
  <p>I have a booking listing page. In this listing page data from "bookings" and "users" are showing.</p>
  
  <p>In this listing page I have a text box to search <code>invoice_number, payment_type, txid and usrEmail</code>. I have written query to search <code>invoice_number, payment_type, txid</code> and it is working but I am stuck on <code>usrEmail</code> section. How can I combine with <code>users</code> and write search query to search <code>usrEmail</code>?</p>
  
  <p>I am using laravel and mongodb. I am following "<a href="https://github.com/jenssegers/laravel-mongodb" rel="nofollow noreferrer">https://github.com/jenssegers/laravel-mongodb</a>"</p>
  
  <p><strong>bookings</strong></p>
  
  <p><code>_id, user, invoice_number, payment_type, txid</code></p>
  
  <p><strong>users</strong></p>
  
  <p><code>_id, firstName, SecondName, usrEmail</code></p>
  
  <p><strong>Search Query</strong></p>
  
  <pre><code>$bookings = Booking::select('_id', 'invoice_number', 'temp_user_id', 'user', 'checkin_from', 'reserve_to', 'beds', 'dormitory', 'sleeps', 'status', 'payment_status', 'payment_type', 'total_prepayment_amount', 'cabinname', 'reference_no', 'clubmember', 'bookingdate', 'txid')
                  -&gt;where('is_delete', 0)
                  -&gt;where(function($query) use ($search) { /* That's the closure */
                      $query-&gt;where('invoice_number', 'like', "%{$search}%")
                          -&gt;orWhere('payment_type', 'like', "%{$search}%")
                          -&gt;orWhere('txid', 'like', "%{$search}%");
                  })
                  -&gt;skip($start)
                  -&gt;take($limit)
                  -&gt;orderBy($order, $dir)
                  -&gt;get();
  </code></pre>
  
  <p><strong>App\Booking</strong></p>
  
  <pre><code>public function user()
      {
          return $this-&gt;hasOne('App\Userlist', 'user', '_id');
      }
  </code></pre>
  
  <p><strong>Images of two collection</strong>
  <strong>User</strong>
  <a href="https://i.stack.imgur.com/kivNN.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/kivNN.jpg" alt="enter image description here"></a>
  <strong>Bookings</strong>
  <a href="https://i.stack.imgur.com/m9jRV.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/m9jRV.jpg" alt="enter image description here"></a></p>
  
  <p><strong>User collection</strong></p>
  
  <pre><code>{ "_id" : ObjectId("57877d23049ac1b819000029"), "usrName" : "adminuser", "usrPassword" : "96a65063135247fef732b5901fe05d1f", "usrFirstname" : "Sarath", "usrLastname" : "TS", "usrTelephone" : null, "usrEmail" : "sara@gmail.com", "usrMobile" : "956209959", "usrAddress" : null, "usrFax" : "4564654", "usrZip" : null, "usrBirthday" : null, "usrDAV" : "316148-DAV-Deutscher Alpenverein", "usrActive" : "1", "usrlId" : "1", "lngId" : "1", "usrPasswordSalt" : ";:?hJM\"9=z/)ea?{%-[**:]68UOT&gt;{gj^{P0+RCF#,Id8c:n+h", "usrRegistrationDate" : ISODate("2016-07-14T11:53:07Z"), "usrRegistrationToken" : "2c2e296bda1661c7fc0645f927d0b17f", "is_delete" : "0", "usrUpdateDate" : ISODate("2017-05-16T09:12:07Z"), "usrPasswordDate" : ISODate("2016-10-25T07:35:57Z"), "usrCity" : null, "usrNewsletter" : null, "money_balance" : "0", "usrCountry" : "Deutschland" }
  </code></pre>
  
  <p><strong>Booking Collection</strong></p>
  
  <pre><code>{ "_id" : ObjectId("58046a49f8f888a80b00002a"), "cabinname" : "Matras-Haus", "checkin_from" : ISODate("2016-10-17T00:00:00Z"), "reserve_to" : ISODate("2016-10-20T00:00:00Z"), "user" : "57877d23049ac1b819000029", "beds" : "2", "dormitory" : "1", "sleeps" : "3", "clubmember" : "1", "status" : "4", "comments" : "", "bookingdate" : ISODate("2016-10-17T06:06:01Z"), "reservation_cancel" : "2", "is_delete" : NumberLong(1), "payment_status" : "1" }
  { "_id" : ObjectId("58183678d2ae67a404431d5c"), "cabinname" : "Kemptner Hütte", "checkin_from" : ISODate("2016-10-31T23:00:00Z"), "reserve_to" : ISODate("2016-11-23T23:00:00Z"), "user" : "57877d23049ac1b819000029", "beds" : "2", "dormitory" : "0", "sleeps" : "2", "clubmember" : "0", "status" : "1", "total_price" : "1288", "payon_cabin" : "1288", "bed_prefer" : "0", "guests" : "2", "comments" : "", "prepayment_amount" : "0", "bookingdate" : ISODate("2016-11-01T06:30:16Z"), "reservation_cancel" : "2", "is_delete" : NumberLong(1), "payment_status" : "0" }
  { "_id" : ObjectId("581b31f3d2ae674d5f431d5b"), "cabinname" : "Kemptner Hütte", "checkin_from" : ISODate("2016-11-07T23:00:00Z"), "reserve_to" : ISODate("2016-11-17T23:00:00Z"), "user" : "57877d23049ac1b819000029", "beds" : "3", "dormitory" : "0", "sleeps" : "3", "clubmember" : "0", "status" : "1", "total_price" : "840", "payon_cabin" : "840", "bed_prefer" : "0", "guests" : "3", "comments" : "", "prepayment_amount" : "0", "bookingdate" : ISODate("2016-11-03T12:47:47Z"), "reservation_cancel" : "2", "is_delete" : NumberLong(1) }
  { "_id" : ObjectId("5821af65d2ae67c82154efc5"), "cabinname" : "Kemptner Hütte", "checkin_from" : ISODate("2017-09-05T22:00:00Z"), "reserve_to" : ISODate("2018-01-24T23:00:00Z"), "user" : "57877d23049ac1b819000029", "sleeps" : "2", "clubmember" : "0", "status" : "1", "total_price" : "5640", "payon_cabin" : "5630", "bed_prefer" : "0", "guests" : "2", "comments" : "", "prepayment_amount" : "21.25", "bookingdate" : ISODate("2016-11-08T10:56:37Z"), "reservation_cancel" : "2", "is_delete" : NumberLong(1) }
  { "_id" : ObjectId("582558d4d2ae679c4d8b4567"), "cabinname" : "2", "checkin_from" : ISODate("2017-07-31T22:00:00Z"), "reserve_to" : ISODate("2017-08-02T22:00:00Z"), "user" : "57877d23049ac1b819000029", "beds" : "", "dormitory" : "", "sleeps" : "2", "clubmember" : "0", "status" : "", "total_price" : "80", "payon_cabin" : "60", "halfboard" : "", "bed_prefer" : "0", "guests" : "2", "prepayment_amount" : "20", "bookingdate" : ISODate("2016-11-11T05:36:20Z"), "is_delete" : NumberLong(1) }
  { "_id" : ObjectId("58352c3cd2ae672341ec89e1"), "cabinname" : "Kemptner Hütte", "checkin_from" : ISODate("2017-05-31T22:00:00Z"), "reserve_to" : ISODate("2017-06-02T22:00:00Z"), "user" : "57877d23049ac1b819000029", "beds" : "", "dormitory" : "", "sleeps" : "2", "clubmember" : "0", "status" : "", "total_price" : "80", "payon_cabin" : "60", "halfboard" : "", "bed_prefer" : "0", "guests" : "2", "prepayment_amount" : "20", "bookingdate" : ISODate("2016-11-23T05:42:20Z"), "reservation_cancel" : "2", "is_delete" : NumberLong(1) }
  </code></pre>