PHP code example of cuongnd88 / lara-guardian

1. Go to this page and download the library: Download cuongnd88/lara-guardian 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/ */

    

cuongnd88 / lara-guardian example snippets


$ composer 


// config/app.php
return [
    // ...
    'providers' => [
        // ...
        Cuongnd88\LaraGuardian\LaraGuardianServiceProvider::class,
    ]
    // ...
];


php artisan vendor:publish --provider="Cuongnd88\LaraQueryKit\LaraQueryKitServiceProvider"

php artisan make:guardian


Route::group(['middleware' => ['guardian']], function(){
    Route::get('/user', function(){
        dump("Congratulation. You have the right permission");
    })->name('user.read');
});

    protected $routeMiddleware = [
    	. . . .
        'guardian' => \App\Http\Middleware\GuardianMiddleware::class,
    ];


namespace App\Models;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use App\Guardian\Traits\HasGuardian;

class User extends Authenticatable
{
    use Notifiable;
    use HasGuardian;
}

    public function joinGroup(Request $request)
    {
        $user = \App\Models\User::find(10);
        $user->joinGroup(2);
    }

    public function joinManyGroups(Request $request)
    {
        $user = \App\Models\User::find(10);
        $user->joinMultiGroups([
            ['group_id' => 1],
            ['group_id' => 3],
        ]);
    }

    public function getUserPermissions(Request $request)
    {
        $user = \App\Models\User::find(10);
        $user->hasPermissions()->toArray();
    }

    public function checkUserAccess(Request $request)
    {
        $user = \App\Models\User::find(10);
        if ($user->rightAccess('product', 'create')) {
        	dump('Right Access');
        } else {
        	dump('Forbidden');
        }
    }

php artisan guardian --action[=ACTION] --model[=MODEL]

php artisan guardian --action=import --model=actions

$data = [
    ['fullname' => 'AAAA', 'email' => '[email protected]', 'age' => 20, 'address' => 'WWW'],
    ['fullname' => 'BBBBB', 'email' => '[email protected]', 'age' => 25, 'address' => 'QQQQ'],
];
\App\Models\User::insertDuplicate(
        $data,
        ['fullname', 'email'],
        ['age', 'address']
    );

	$exceptable = ['created_at', 'updated_at', 'deleted_at'];
	$data = app(User::class)->except($exceptable)->get()->toArray()