【发布时间】:2016-01-28 17:29:13
【问题描述】:
您好,我有这三种型号:
用户.php
<?php namespace App\Models;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
use Kodeine\Acl\Traits\HasRole;
class User extends Model implements AuthenticatableContract, CanResetPasswordContract {
use Authenticatable, CanResetPassword, HasRole;
/**
* The database table used by the model.
*
* @var stringSS
*/
protected $table = 'users';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['name', 'email', 'password', 'is_active'];
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = ['password', 'remember_token'];
public function roles()
{
return $this->belongsToMany('App\Models\Role', 'role_user', 'user_id', 'role_id');
}
public function bankBranch()
{
return $this->belongsToMany('App\Models\BankBranch', 'bank_branches_users', 'user_id', 'branch_id');
}
public function permissions()
{
return $this->belongsToMany('App\Models\Permissions', 'permission_user', 'user_id', 'permission_id');
}
}
Bank.php
<?php namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Bank extends Model {
protected $table = 'bank_details';
public function branches()
{
return $this->hasMany('App\Models\BankBranch', 'bank_id');
}
public function users()
{
return $this->hasManyThrough('App\Models\User', 'App\Models\BankBranch');
}
}
BankBranch.php
<?php namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class BankBranch extends Model {
protected $table = 'bank_branches';
public function bank()
{
return $this->belongsTo('App\Models\Bank', 'bank_id');
}
public function users()
{
return $this->hasMany('App\Models\User', 'bank_branches_users', 'branch_id', 'user_id');
}
}
好的,现在在我的应用程序中,我有以下关系: 1. 用户属于多家银行分行。 2.银行分行属于一家银行。 3. BankBranch有很多用户。
现在,当用户登录时,我希望他们只能看到与该用户位于同一银行分行的其他用户。
在我的 admin->user 页面上的意思是,我想要一个与登录用户位于同一分支的用户列表。
除非登录用户属于许多其他分支,否则应该显示登录用户所属分支中的所有用户。
我在雄辩的模型中表示这一点并通过我的控制器获取数据时遇到了很大的困难。
【问题讨论】: