mardi 27 septembre 2016

How define relation hasManyThrough

I have tables:

users
 - id
 - name
 - company_id
companies
 - id
 - company_name
watched_objects
 - id
 - user_id
 - object_id
 - type

Now I want to get all watched companies for a user.

So query should looks:

SELECT
  companies.*
FROM companies
  JOIN watched_objects ON watched_objects.object_id = companies.id
WHERE watched_objects.user_id = 1

How should I define relations?

I try this:

class User
{
    public function watched()
    {
        return $this->hasManyThrough('App\Company', 'App\WatchedObject', 'user_id', 'id');
    }
}

But query is:

SELECT
  companies.*,
  watched_objects.user_id
FROM companies
  INNER JOIN watched_objects ON watched_objects.id = companies.id
WHERE watched_objects.user_id = 1

How I can change watched_objects.id to watched_objects.object_id.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire