I have four tables which is departments
, users
, items
, items_inventories
The relationship is like this:
A user
has a assigned department
. An item
has a assigned department
. item_inventories
has many items.
Structure:
users
->id
->name
->password
->access_type (department_id)
departments
->id
->name
items
->id
->name
->department_id
items_inventories
->id
->item_id
->qty
My models:
class Item extends Model
{
public function department()
{
return $this->hasOne('App\Http\Models\Department', "id", "department_id");
}
}
class ItemsInventory extends Model
{
public function item()
{
return $this->hasOne('App\Http\Models\Item', "id", "item_id");
}
}
In my items_inventories
how do I query all items that belongs to a specific department? Since items
has already a relationship to departments
, How do I query like: select all items in items_inventories where item department_id is equal to 3?
My goal is, I have a user who is logged in, and I can access the assigned department to him/her via access_type
(department_id) when my page loads, I want to list only items in the items_inventories
that is assigned to his/her department. I already checked: https://laravel.com/docs/5.4/eloquent-relationships#relationship-methods-vs-dynamic-properties
but can't seem to find something that matches my requirement. Thanks
via Yassi