Luke Whitehouse
Luke Whitehouse

Reputation: 33

Eloquent/Query Builder for more complicated table structures

OK so this probably isn't the biggest problem in the world and I'm sure someone will be able to help me out with where I'm going wrong. Here's a few facts before I get to my problem:

The problem

I'm currently trying to search the locations (via a form – i.e. Input::get('data')), and this would bring up all events that have that location, and all meetups that associate with that event.

Within my SearchController, I have the following:

// Get the searched data
$data = Input::get('data');

// Get all location, which are 'LIKE' the inputted data.
$locations = Location::where('title', 'LIKE', '%'. $data .'%')->get();

At this point I'm not sure how to proceed. I need to access the events from each location and after that I'd need a get the meetup that corresponds with that location, but I'm not sure how to go about this. Can this be done with joins() or would I loop through the $locations variable and get the ->events for each of those and put them into an array? Or, should I be using DB:: and running a custom query?

Upvotes: 0

Views: 110

Answers (1)

Luke Whitehouse
Luke Whitehouse

Reputation: 33

See @iavery's comment, it seems the following works when you have all belongsToMany relationships:

Location::where( ... )->with('events.meetups')->get();

Upvotes: 1

Related Questions