Php 模型与laravel 4有关

Php 模型与laravel 4有关,php,laravel,relationship,Php,Laravel,Relationship,我希望你一切顺利,我需要你的帮助,我正在与Laravel合作,我需要关联信息,我有用户、类型成本和成本中心模型,其中关系如下: user: hasMany ('TypeCost'); belongsTo ('CostCenter', 'id'); TypeCost: belongsTo ('User'); CostCenter: hasMany ('User'); 我在TypeCost模块中有我的视图索引,有来自每个用户成本的加载信息,我需要的是,如果登录的用户属于CostCenter,则

我希望你一切顺利,我需要你的帮助,我正在与Laravel合作,我需要关联信息,我有用户、类型成本和成本中心模型,其中关系如下:

user:
hasMany ('TypeCost');
belongsTo ('CostCenter', 'id');

TypeCost:
belongsTo ('User');

CostCenter:
hasMany ('User');
我在TypeCost模块中有我的视图索引,有来自每个用户成本的加载信息,我需要的是,如果登录的用户属于CostCenter,则只需显示属于CostCenter 1的用户的记录,以及CostCenter 2、3等的记录

这是我的索引方法,用于显示费用列表:

public function index ()
{
$ type_costs = TypeCost :: paginate ();
return View :: make ('type_costs.index' compact ('type_costs'));
}
它将返回属于已登录用户的成本中心的用户列表。
如果用户表中costcenter\u id字段可为空,则可以在控制器上添加一些验证。

为什么要使用
compact
而不是
View::make()->with()
?使用脚手架创建控制器。感谢我所有的朋友,我会尝试的。
//for user model
public function costcenter()
{
  $this->belongsTo('CostCenter');
}
 //for costcenter model
public function users()
{
  $this->hasMany('User');
}

//controller function
public function index ()
{
  $cost_centers = Auth::user()->costcenter;
  $cost_users = $cost_centers->users;
  return View::make ('type_costs.index', compact ('cost_users'));
}