PHP code example of bfg / permission

1. Go to this page and download the library: Download bfg/permission library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

bfg / permission example snippets




namespace App\Models;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use App\Models\Traits\User\UserHasRole;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Bfg\Permission\Traits\Permissions;

/**
 * User Class
 * @package App\Models
 */
class User extends Authenticatable
{
    use Notifiable, 
        UserHasRole, 
        HasFactory, 
        Permissions; // Like this
    
    // ...
}



namespace App\Http\Controllers;

use App\Models\Message;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;

class MessageController extends Controller
{
    /**
     * Update the given message.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Message  $message
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Message $message)
    {
        if (! Gate::allows('update-message', $message)) {
            abort(403);
        }

        // Update the message...
    }
}

if (Gate::forUser($user)->allows('update-message', $message)) {
    // The user can update the message...
}

if (Gate::forUser($user)->denies('update-message', $message)) {
    // The user can't update the message...
}

if (Gate::any(['update-post', 'delete-message'], $message)) {
    // The user can update or delete the message...
}

if (Gate::none(['update-post', 'delete-message'], $message)) {
    // The user can't update or delete the message...
}

Gate::authorize('update-message', $message);
bash
php artisan install bfg/permission
bash
php artisan vendor:publish --tag=permission-config
bash
php artisan vendor:publish --tag=permission-migrations
bash
php artisan permissions message
bash
php artisan allow message -r
bash
php artisan allow message guest -r
bash
php artisan disallow message guest -r
bash
php artisan permission:delete view-message
bash
php artisan permission:delete message -r