0

我有两个资源

  • 组织

  • 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

在转到该资源的索引时,我不想看到所有组织,而是希望它列出经过身份验证的用户是组织用户之一的所有组织。

怎么可能呢?

这个问题与这个问题有相似之处,但有不同的看法。

4

1 回答 1

2

如果我知道你是真的,你可以使用这个:

$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

但是为了更准确的响应,您应该详细解释问题。

于 2021-02-27T09:06:26.553 回答