Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

sarathiscookie's avatar

How to take matching data for searching using laravel and mongo?

I have two collections bookings and users. In bookings collection I have a field *user. It is related to users._id.

I have a booking listing page. In this listing page data from bookings and users are showing.

I have a text box to search invoice_number, payment_type, txid and usrEmail. I have written query to search invoice_number, payment_type, txid and it is working but I am stuck on usrEmail section. How can I combine with users and write search query to search usrEmail?

I am using laravel and mongodb. I am following "https://github.com/jenssegers/laravel-mongodb"

bookings

_id, user, invoice_number, payment_type, txid

users

_id, firstName, SecondName, usrEmail

Search Query

$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')
                ->where('is_delete', 0)
                ->where(function($query) use ($search) { /* That's the closure */
                    $query->where('invoice_number', 'like', "%{$search}%")
                        ->orWhere('payment_type', 'like', "%{$search}%")
                        ->orWhere('txid', 'like', "%{$search}%");
                })
                ->skip($start)
                ->take($limit)
                ->orderBy($order, $dir)
                ->get();

App\Booking

public function user()
    {
        return $this->hasOne('App\Userlist', 'user', '_id');
    }
0 likes
1 reply
bipin's avatar

1.join both table and then perform search 2.suppose if your are performing search on table(booking) then you will also get userid. stored it in any other variable and again perform query using where condition on user table

Please or to participate in this conversation.