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

mathishuettl's avatar

Where clause on two relationships

Hello,

I got a Database like this:

Customers: id, name

Inquiries: id customer_id

Bookings: id inquiry_id

Now i want to search all customers who made a booking, but i don't know how... can you help me?

0 likes
8 replies
tisuchi's avatar

Think Relationships-

  • A Customer has Many Inquiries
  • An Inquiry has has one / many bookings (you decide)

Now echoing @m165437 answer-

// Retrieve all customers that have at least one booking... $customersWithBookings = App\Customer::has('bookings')->get();

3 likes
mathishuettl's avatar

Thanks guys, but i get a errormessage

BadMethodCallException Call to undefined method App\Customer::has()

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Customer extends Model
{
    public function inquiries() {
        return $this->hasMany(Inquiry::class);
    }
}

Cronix's avatar

can't get bookings without going through inquiries first, since a booking belongs to an inquiry and not the customer (directly)

mathishuettl's avatar

okay thanks, but this doesn't solve the problem because i still get a methodnotfound exception

Please or to participate in this conversation.