dongpu3898 2014-10-09 16:14 采纳率: 100%
浏览 20

Laravel 4加载与最新和另一个条件的关系

Hi guys I have here a small code that will load a user status with latest. My question is how can I extend this so that when example user 1 and user 2 are the same department id of 2 they can see there statuses. Just like facebook, when user 1 is friend with user 2, user 1 can see user 2 status and user 2 can also see user 1 status.

$user = Auth::user()->load(['statuses' => function($status)
{
    $status->latest();
}]);

TIA :)

  • 写回答

1条回答 默认 最新

  • 普通网友 2014-10-09 19:39
    关注

    Department hasMany Users, and I assume User hasMany Statuses, so this will work:

    // Department
    public function statuses()
    {
      return $this->hasManyThrough('Status', 'User');
    }
    
    // Status
    public function user()
    {
      return $this->belongsTo('User');
    }
    
    // User
    public function department()
    {
      return $this->belongsTo('Department');
    }
    
    // usage
    $statuses = Auth::user()->department->statuses()->with('user')->latest()->get();
    
    评论

报告相同问题?

悬赏问题

  • ¥20 易康econgnition精度验证
  • ¥15 线程问题判断多次进入
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致