dwi-rengga/org
Laravel package scaffolding for Dwi Rengga organization projects, providing shared configuration, conventions, and reusable components to speed up new app and package setup within the org.
Installation
composer require dwi-rengga/org
Publish the migration and config (if available):
php artisan vendor:publish --provider="DwiRengga\Org\OrgServiceProvider"
Run migrations:
php artisan migrate
First Use Case: Define an Organization Register the organization model and seed initial data:
use DwiRengga\Org\Models\Organization;
// Create a new org
$org = Organization::create([
'name' => 'Acme Corp',
'slug' => 'acme',
'description' => 'Global leader in widgets',
]);
Key Files to Review
config/org.php (if published)app/Models/Organization.php (or vendor/dwi-rengga/org/src/Models/Organization.php)database/migrations/ for schema detailsHierarchical Structure Management
Use the hasMany/belongsTo relationships for departments, teams, or roles:
// Define relationships in Organization model
public function departments()
{
return $this->hasMany(Department::class);
}
// Create a department under an org
$org->departments()->create(['name' => 'Engineering']);
User-Organization Assignment
Extend the User model to link to organizations:
// In User model
public function organizations()
{
return $this->belongsToMany(Organization::class, 'user_organization');
}
// Assign a user to an org
$user->organizations()->attach($org->id, ['role' => 'admin']);
API Resource Integration Generate a resource for RESTful endpoints:
php artisan make:resource OrgResource --model=DwiRengga\Org\Models\Organization
Customize fields in OrgResource:
public function toArray($request)
{
return [
'id' => $this->id,
'name' => $this->name,
'departments_count' => $this->departments()->count(),
];
}
Policy-Based Access Control Create policies for org-specific permissions:
php artisan make:policy OrgPolicy --model=Organization
Example:
public function viewAny(User $user)
{
return $user->hasRole(['admin', 'manager']);
}
php artisan vendor:publish --tag=org-nova
public function scopeActive($query)
{
return $query->where('status', 'active');
}
Organization::observe(OrgObserver::class);
Missing Middleware
Ensure org middleware is registered in app/Http/Kernel.php if using route groups:
'org' => \DwiRengga\Org\Http\Middleware\OrgMiddleware::class,
Usage:
Route::middleware(['org'])->group(function () {
// Org-specific routes
});
Slug Conflicts Validate slugs during creation to avoid duplicates:
$validated = $request->validate([
'slug' => 'required|unique:organizations,slug',
]);
Caching Quirks Clear caches after schema changes:
php artisan cache:clear
php artisan config:clear
DEBUG=true
registerModelEvent for debugging:
Organization::registerModelEvent(
'saving',
function ($org) {
\Log::debug('Saving org:', [$org->toArray()]);
}
);
Custom Fields
Extend the Organization model with traits or add columns via migrations:
Schema::table('organizations', function (Blueprint $table) {
$table->string('industry')->nullable();
});
Event Listeners
Subscribe to org events (e.g., OrgCreated):
public function handle(OrgCreated $event)
{
// Send welcome email
}
Testing Use factories for testing:
$org = Organization::factory()->create();
Or seed test data:
$this->seedOrganizations();
use Illuminate\Database\Eloquent\SoftDeletes;
class Organization extends Model
{
use SoftDeletes;
protected $dates = ['deleted_at'];
}
Route::middleware(['throttle:60,1'])->group(...);
localization package integration.How can I help you explore Laravel packages today?