Coder Social home page Coder Social logo

chrisrhymes / policy-collect Goto Github PK

View Code? Open in Web Editor NEW
10.0 3.0 0.0 9 KB

This Laravel package offers a collection method that will go through a collection of models and check against the methods on the policy

PHP 100.00%
laravel-package laravel collection laravel6 laravel-collections

policy-collect's Introduction

Policy Collect

Imagine you have a collection of models that you are returning via an ajax call, meaning you don't have access to the @can blade helpers to see whether the user should be able to view, update or delete each model.

This package offers a collection method that will go through a collection of models and check against the standard resource methods on the policy, returning whether the logged in user can perform an action. This can then be used in your JavaScript to decide which buttons to show or enable.

Installation

composer require chrisrhymes/policy-collect

Usage

For example, if the policy checks an order belongs to a user whether they can view the order and the first order does belong to the user then you can do the following:

$orders = Order::paginate()->policy();

echo $orders[0]->can->view; // true 

Resource Methods

It will return the standard policy methods by default

$orders[0]->can->view;
$orders[0]->can->update;
$orders[0]->can->delete;
$orders[0]->can->restore;
$orders[0]->can->forceDelete;

Additional Policy Methods

The policy collection method also allows you to pass in an array of custom methods to check these in your policy as well.

For example, if you had a refund method on your order policy to determine who can refund the order, which only allows admin users to refund you can do the following:

$orders = Order::paginate()->policy(['refund']);

echo $orders[0]->can->refund; // false

policy-collect's People

Contributors

chrisrhymes avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.