Installation
composer require vizrex/laratrust-ingest
Ensure santigarcor/laratrust is also installed (required dependency).
Publish Configuration (if needed)
php artisan vendor:publish --provider="Vizrex\LaratrustIngest\LaratrustIngestServiceProvider"
(Note: The package may not require config; verify via config/laratrust-ingest.php if it exists.)
Prepare CSV File
permission_name,permission_display_name,permission_description,super-admin;Super Admin,admin;Admin
create_post,Create Post,Allows users to create posts,y,n
Run the Command
php artisan roles-and-permissions:update /path/to/your/file.csv
permissions, roles, and their pivot tables. Backup data first.database/seeds/ for dynamic systems.Design CSV Structure
super-admin, editor, viewer).y/n for granular permission assignment (e.g., y for edit_post, n for delete_post).Version Control CSV Files
storage/permissions/ or resources/data/.permissions_v1.csv, permissions_v2.csv).Automate with Artisan
post-deploy scripts (e.g., Laravel Forge/Envoyer):
php artisan roles-and-permissions:update storage/permissions/latest.csv
Artisan::call() in a custom command for pre-processing (e.g., validate CSV before truncation).Integration with Laratrust
hasRole() and hasPermission() in policies/controllers:
if (auth()->user()->hasPermission('create_post')) {
// Allow action
}
Dynamic Role Assignment
Combine with Laravel’s Event system to auto-assign roles on user creation:
// In UserObserver or service provider
event(new Registered($user));
$user->attachRole('default'); // Assign role post-import
Partial Updates
admin_permissions.csv, user_permissions.csv).Testing
Use php artisan roles-and-permissions:update tests/data/test_permissions.csv in phpunit.xml:
<env key="DB_DATABASE" value="laratrust_testing"/>
Data Loss Warning
php artisan db:backup (spatie/laravel-backup).Role::create() and Permission::create() for incremental updates.CSV Parsing Errors
league/csv before ingestion.admin instead of admin;Admin).Laratrust Version Mismatch
Class 'Laratrust\Permission' not found.santigarcor/laratrust is installed and configured in config/laratrust.php.Case Sensitivity
Super-Admin vs. super-admin).Enable Query Logging
Add to AppServiceProvider:
public function boot()
{
if (app()->environment('local')) {
DB::enableQueryLog();
}
}
Check logs after running the command to verify SQL queries.
Dry Run Mode
php artisan roles-and-permissions:update file.csv --dry-run # Not native; use a wrapper command
public function handle()
{
$csv = new \League\Csv\Reader(fopen($this->argument('csvFilePath'), 'r'));
// Parse and log changes without DB operations
}
Custom Validation Extend the package by overriding the command:
// app/Console/Commands/UpdateRolesAndPermissions.php
namespace App\Console\Commands;
use Vizrex\LaratrustIngest\Commands\UpdateRolesAndPermissions as BaseCommand;
class UpdateRolesAndPermissions extends BaseCommand
{
protected function validateCsv(array $data)
{
// Add custom rules (e.g., reject empty permission names)
}
}
Post-Processing Hooks
Use Laravel’s events to trigger actions after ingestion:
// In a service provider
\Vizrex\LaratrustIngest\Events\RolesPermissionsUpdated::listen(function ($roles, $permissions) {
// Sync with external systems (e.g., API calls)
});
GUI Alternative
For non-technical users, pair with a package like spatie/laravel-permission + a form builder (e.g., Nova/Vue) to manually assign permissions.
config/laratrust.php).lt_), ensure the CSV logic accounts for them (unlikely, but verify with php artisan schema:dump).max_execution_time in php.ini or chunk the import:
// In the package’s logic, add batch processing
DB::statement('SET SESSION max_execution_time=300;');
How can I help you explore Laravel packages today?