php - Laravel 资源索引列出有限数量的记录
问题描述
我有两个资源
组织
OrganizationUsers(对用户有 FK
user_id
)
用户模型
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
在转到该资源的索引时,我不想看到所有组织,而是希望它列出经过身份验证的用户是组织用户之一的所有组织。
怎么可能呢?
这个问题与这个问题有相似之处,但有不同的看法。
解决方案
如果我知道你是真的,你可以使用这个:
$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
但是为了更准确的响应,您应该详细解释问题。
推荐阅读
- docker - 如何使用 ssh 连接获取卷数据
- spring-boot - 如何将 Spring-boot 调度器设计为 Service,以便其他微服务可以使用它
- python - Smtplib 代码在 python3.7 中适用于 linux 但不适用于 windows 10
- sql - ORA-01843: 在 maximo 上作为报告运行时不是有效的月份异常
- javascript - 如何减少drupal网站的TTFB时间
- python - 用美丽的汤循环麻烦
- sql - SQL SERVER中的select查询需要支持
- ios - Swift:如何为其委托创建一个具有泛型类型的自定义覆盖 UITableView?
- powershell - 映射后在资源管理器中打开驱动器
- r - 组间相关性