Laravel table relationship

Viewed 11875

I have 3 tables:

users
leads
users_leads

On users table:

---------------
-- id | name --
-- 1  | doe  --
-- 2  | joe  --
---------------

On leads table:

----------------
-- id | type  --
-- 1  | house --
-- 2  | condo --
-- 3  | house --
----------------

On users_leads table:

----------------------------
-- id | user_id | lead_id --
-- 1  | 1       | 1       --
-- 1  | 1       | 2       --
-- 1  | 2       | 3       --
----------------------------

Then I have my models:

User.php
Lead.php
UsersLead.php

On model/UsersLead.php

class UsersLead extends Eloquent {
   public function leads()
   {
      return $this->belongsTo('Lead');
   }
}

On model/Lead.php

class Lead extends Eloquent {
   public function userslead()
   {
      return $this->hasMany('UsersLead');
   }
}

Then I tried to run it:

$data = Lead::all()->userslead;

Laravel is throwing this error:

Undefined property: Illuminate\Database\Eloquent\Collection::$userslead

how do I run the query using eloquent ORM. To get all lead that belongs to specific user?

Anyone can help? Thanks.

2 Answers
Related