Search code examples
phplaravellaravel-routinglaravel-viewslaravel-resource

Laravel resource index listing limited amount of records


I have two resources

  • Organizations

  • OrganizationUsers (which has FK to Users on user_id)

The User model

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);
    }
}

, the Organization model is

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);
    }
}

and the OrganizationUser model is

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;
    }

}

When it comes to access these resources

Route::resource('organization', 'OrganizationController');
Route::resource('organizationuser', 'OrganizationUserController', ['except' => ['show']]);

Also, this is the index() of the OrganizationController

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()]);
    }
    
    ...
}

and the navigation

@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

Instead of seeing all of Organizations when going to the index of that resource, I'd like it to list all Organizations where the authenticated user is one of the OrganizationUsers.

How can that be done?

This question has similarities with this one but it's with a different take.


Solution

  • If I know you true, you can use this:

    $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
    

    But for more accurate response you should explain problem in details.