I have 2 tables vendors
and partners
vendors
table only stores the name of some company and partners
table store users who work for some company. So the structure is somethign like this:
vendors
| id | name |
+------+---------------+
| 1 | Vendor-1 |
| 2 | Vendor-2 |
| 3 | Vendor-3 |
Partners
| id | user_name | password |vendor_id | is_owner | is_admin | is_technitian |
+----+------------+-----------+----------+------------+------------+---------------+
| 1 | abc | ^&ASKJHA | 1 | 1 | 1 | 0 |
| 2 | def | ^&ASKJHA | 2 | 1 | 1 | 0 |
| 3 | ghi | ^&ASKJHA | 1 | 0 | 1 | 0 |
| 4 | jkl | ^&ASKJHA | 3 | 1 | 1 | 0 |
| 5 | mno | ^&ASKJHA | 1 | 0 | 0 | 1 |
| 6 | pqr | ^&ASKJHA | 2 | 0 | 1 | 0 |
| 7 | stu | ^&ASKJHA | 1 | 0 | 0 | 1 |
| 8 | vwx | ^&ASKJHA | 2 | 0 | 0 | 1 |
| 9 | yz | ^&ASKJHA | 3 | 0 | 0 | 1 |
So as you can see above that One partner is the owner of any vendor and rest of them work as employees for the vendor.
I am working with Eloquent ORM and i have already defined Models for both Partner and vendor. I want to add an owner
method in the Vendor Model so i can directly access the owner for any vendor Object. What i want to know is how do i relate this in my model defination. Is it do-able or do i need to make some changes in my database structure ?
class Vendor extends Model{
/**
* Get all the users for this vendor
*/
public function users(){
$this->hasMany(Partner::class);
}
public function owner(){
// how do i relate one owner from partner model who has is_owner == 1
}
}
via Angry Coder