dimanche 29 octobre 2017

Laravel 5 Three-way One-to-Many Eloquent Relationship

I have database like this
opportunities

  • Id
  • contact_id

contacts

  • Id
  • User_id

users

  • id

User model :

class User extends Authenticatable
{

public function Contact()
{
    return $this->hasMany('Customer\model\Contact');
} 
}

Contact model:

 class Contact extends Model
{
    public function Opportunity()
   {
    return $this->hasMany('Sale\Model\Opportunity');
   }
  public function User()
{
    return $this->belongsTo('App\User');
} 
}

Opportunity model

class Opportunity extends Model
 {
       public function Contact()
    {
      return $this->belongsTo('Customer\Model\Contact');
    }
  public function User()
  {
   return $this->hasManyThrough('App\User','Customer\Model\Contact');
  }  
}

When on controller

  $Opportunity = Opportunity::with('User')->paginate(10);
  print_r($Opportunity->User);  

Show me wrong data.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire