John Davidson

php - Laravel resource index listing limited amount of records

0 comments
Message:


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.



My Answer:
>belongsTo('App\Role', 'role_id');_ }__ /**_ * Get the organizations that the user belongs to_ *_ * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany_ */_ public function organizations()_ {_ return $this->belongsToMany('App\Organization', 'organization_users', 'user_id', 'organization_id');_ }}

In the Organization model:

php
class Organization extends Model
{
protected $fillable = ['name', 'description'];

public function users()
{
return $this->belongsToMany('App\User', 'organization_users', 'organization_id', 'user_id');
}
}


Now, in your OrganizationsController, you can retrieve a limited amount of organizations with their users like this:

php
use App\Organization;

public function index()
{
$organizations = Organization::with('users')->take(10)->get();

return response()->json($organizations);
}


This code will retrieve the first 10 organizations along with their users. You can adjust the `take(10)` method to retrieve a different number of organizations.

Rate this post

4 of 5 based on 2424 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us