I'm trying to achieve something that is similar to Laravel's hasManyThrough
, but I'm not sure my DB is set up appropriately or I'm just missing something.
I am trying to display a page for admins to show all of the sites we support. I would like to have a simple column that shows a distinct count of how many customers are attached to each site. To do this, I was going to go through the orders
table and retrieve a distinct
list of users, then simply use the ->count()
method inside my view.
Here is my DB setup (simplified):
sites
table (primary key: 'id'):
id | ...
users
table (primary key: 'id'):
id | first_name | last_name | ...
orders
table (primary key: 'order'):
id | order | user_id | site_id | ....
Site
model:
public function customers()
{
return $this->hasManyThrough('App\User', 'App\Order', 'site_id', ' id')->distinct();
}
I realize right away that the key difference between my DB setup and the documentation is I do not have an order_id
in my users
table, but it doesn't make sense that I do since a user can have many orders.
It is worth noting: I also have a table user_orders
. I'm not sure if I should be using that instead. user_orders
has the following set up:
id | user_id | order
You can see that it is simply an intermediate table to hold connections between users and orders (remember order
is the PK in orders, not id).
So, can anyone help me understand what I am doing wrong?
via Mark