mardi 27 octobre 2015

Laravel 5 Eloquent relationships through table

Need help with Laravel 5 eloquent relationships.

Let's keep it simple, I have three tables users, offers and managers.

users (id, name, type, ...), offers (id, user_id, ...), managers (company_id, manager_id, ...),

User can be manager of company or company.

User model has many offers (I think need to fix here something)

public function offers()
{
    return $this->hasMany('App\Offer');
}

User has many managers

public function managers()
{
    return $this->hasMany('App\Manager', 'company_id');
}

Manager belongs to User per manager_id as manager

public function user()
{
    return $this->belongsTo('App\User', 'manager_id');
}

Manager belongs to User per company_id as company

public function company()
{
    return $this->belongsTo('App\User', 'company_id');
}

What I want is to get my offers as manager and company offers together.

For example:

User table has two users:

id name type

100 SMART company

200 Bob manger

300 GO company

Managers table has:

company_id manager_id

100 200

Offers table has:

id user_id name

1 100 Testas

2 200 Testas2

3 300 Testas3

I don't understand how can I get all my and SMART company offers as Bob manager. Result should be two offers Testas and Testas2? Method offers() will return only my offers.

$user = \Auth->user();
$user->offers();

To cut a long story short, if I join as company I need my offers. If I join as manager I need my offers and offers of my company.

Maybe I need here hasManyThrough relation, but how to use it in my example?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1NzaiiK
via IFTTT

Aucun commentaire:

Enregistrer un commentaire