有没有办法用hasMany链接ManytoMany查询? 感谢
$user=User::find(Auth::Id());
$pagination=$user->emails()->where('email.draft','<>',true)->
where('email.trash','<>',true)->
orderby('date_time','desc')->paginate(10);
foreach ($pagination as $email)
{
echo $email->pivot->sender_UID;//sender_UID references users table
echo $email->message;
}
以上查询没有多对多的关系并且工作正常但我想将hasMany关系结果与同一查询绑定。 E.x sender_UID与users表有多对一的关系,所以我希望查询也抓住用户的f_name和l_name以及相应的sender_UID所以foreach会打印出类似这样的东西..
foreach ($pagination as $email)
{
echo $email->f_name;
echo $email->message;
}
//User Model
class User extends Eloquent {
public function emails()
{
return $this->belongsToMany('Email', 'users_email', 'Uid','email_id')->
withPivot('sender_UID');
}
public function userinfo()
{
return $this->hasMany('User_email','sender_UID');
}
}
// Email Model
class Email extends Eloquent
{
public function attach()
{
return $this->hasMany('Attachments','email_id');
}
public function users()
{
return $this->belongsToMany('User', 'users_email', 'email_id','Uid');
}
}
//User_Email Model
class User_email extends Eloquent
{
protected $table="users_Email";
protected $primaryKey="Id";
public function senderinfo()
{
return $this->belongsTo('User','Id','sender_UID');
}
}
}