如何解决Laravel 资源索引列出了有限数量的记录
我有两个资源
-
组织
-
OrganizationUsers(对
user_id
上的用户有 FK)
用户模型
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name','email','password','picture','role_id'
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password','remember_token',];
/**
* Get the role of the user
*
* @return \App\Role
*/
public function role()
{
return $this->belongsTo(Role::class);
}
/**
* Get the path to the profile picture
*
* @return string
*/
public function profilePicture()
{
if ($this->picture) {
return "/{$this->picture}";
}
return 'http://i.pravatar.cc/200';
}
/**
* Check if the user has admin role
*
* @return boolean
*/
public function isAdmin()
{
return $this->role_id == 1;
}
/**
* Check if the user has creator role
*
* @return boolean
*/
public function isCreator()
{
return $this->role_id == 2;
}
/**
* Check if the user has user role
*
* @return boolean
*/
public function isMember()
{
return $this->role_id == 3;
}
/**
* The organization_user that belong to the user.
*
* @return \Illuminate\Database\Eloquent\Collection
*/
public function organizations()
{
return $this->belongsToMany(OrganizationUser::class);
}
}
,组织模型是
class Organization extends Model
{
protected $fillable = [
'user_id','name','slug','is_visible'
];
/**
* Get the user owner of the organization
*
* @return \App\User
*/
public function user()
{
return $this->belongsTo(User::class);
}
}
而 OrganizationUser 模型是
class OrganizationUser extends Model
{
protected $fillable = [
'user_id','organization_id','is_admin','name'
];
/**
* Get the user
*
* @return \App\User
*/
public function user()
{
return $this->belongsToMany(User::class);
}
/**
* Get the organization
*
* @return \Organization
*/
public function organization()
{
return $this->belongsTo(Organization::class);
}
/**
* Check if the user is_admin
*
* @return boolean
*/
public function isAdmin()
{
return $this->is_admin == 1;
}
}
谈到访问这些资源
Route::resource('organization','OrganizationController');
Route::resource('organizationuser','OrganizationUserController',['except' => ['show']]);
此外,这是 OrganizationController 的 index()
class OrganizationController extends Controller
{
/**
* display a listing of the organizations
*
* @param \App\Organization $model
* @return \Illuminate\View\View
*/
public function index(Organization $model)
{
$this->authorize('manage-users',User::class);
return view('organizations.index',['organizations' => $model->all()]);
}
...
}
和导航
@can('manage-organizations',App\User::class)
<li class="nav-item {{ $elementName == 'organization-management' ? 'active' : '' }}">
<a class="nav-link" href="{{ route('organization.index') }}">
<i class="ni ni-briefcase-24" style="color: #1E73BE;"></i>
<span class="nav-link-text">{{ __('Organizations') }}</span>
</a>
</li>
@endcan
在访问该资源的索引时,与其查看所有组织,不如列出所有经过身份验证的用户是组织用户之一的组织。
这怎么办?
这个问题与 this one 有相似之处,但有不同的看法。
解决方法
如果我知道你是真的,你可以用这个:
$result = Organization::whereIn('id',function($query){
$query->select('organization_id')
->from(with(new OrganizationUser)->getTable())
->where('user_id',Auth::user()->id);
})->get();
// now your result is in $result
但为了更准确的回答,您应该详细解释问题。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。