Skip to content

Add global scopes for restrinction #19

@ccarlosm

Description

@ccarlosm

This must be adapted if admins can create for any users

If in need to restrict data for a certain authenticated user, we can use global scopes in model in the booted() method.

EXAMPLE:
Model Tasks
(Show only tasks created by the user authenticated)

protected static function booted(): void
{
//Add a global scope for showing only the tasks created by the authenticated user
static::addGlobalScope('creator', function (Builder $builder) {
$builder->where('creator_id', Auth::id());
});
}

VIDEO 14 - Laravel 10 For REST API's

This goes perfect for: index, show, delete, update. But for store, we must modify it like:

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions