Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Permissions For Laravel Laravel Package

roundly-consulting/permissions-for-laravel

A Laravel package for adding role and permission management to your application. Define roles, assign permissions, and control access through simple, familiar APIs. Lightweight and easy to integrate, it helps keep authorization logic clear and maintainable in existing Laravel projects.

View on GitHub
Deep Wiki
Context7

Getting Started

  1. Install via Composer: composer require <vendor>/<package> (replace with actual package name).
  2. Publish the config file: php artisan vendor:publish --tag=<package>-config (or check the package README for the exact tag).
  3. Open config/<package>.php and register the models you want factories to build. The key mapping is usually 'models' => [ ... ] or similar.
  4. Define your factories using the package’s base factory class (or trait), and reference the configured model key rather than a hard‑coded class.
  5. First use case: in a test or seeder, call the factory for a model and confirm it returns an instance of the configured class. Then try swapping the model via the package’s host‑swap API (e.g., Package::swapModel('user', CustomUser::class)) and create another instance—it should now build CustomUser instead of the default.

Where to look first: the package’s config file, its service provider (to see how model resolution is bound), and any Factory base class or trait it ships. The 1.0.2 fix means that after a host swap, factories build the configured model, so test that path early.

Implementation Patterns

  • Config‑driven model resolution: Always reference models through the package’s configuration rather than instantiating them directly in factories. This lets you swap implementations per environment, test case, or tenant without touching factory code.
  • Host‑based swapping in tests: Use the package’s swap mechanism in setUp() or a test helper to replace a model with a fake or extended version. Factories will then build the swapped model (fixed in 1.0.2), keeping test data consistent with the swapped class.
  • Multi‑tenant / multi‑host workflows: If your app swaps models based on the current host (e.g., tenant‑specific user models), register the swap early in the request lifecycle (middleware or service provider) before any factory calls. Factories will respect the active mapping.
  • Seeding and factories: In seeders, resolve factories through the package so that any host‑specific model swap applies. Avoid new User or User::factory() directly if you rely on the package’s model configuration.
  • Extension points: Most packages of this kind expose a Factory base class or a HasFactory trait. Override modelClass() or equivalent to return the configured model key, and use the package’s resolver rather than resolve(Model::class).
  • Integration with Laravel’s container: The package likely binds a model resolver in the container. If you need custom resolution logic, bind your own implementation after the package’s service provider runs, or use the provided swap API rather than rebinding.

Gotchas and Tips

  • Upgrade to 1.0.2 if you swap models per host: Before 1.0.2, factories could ignore a host swap and build the original model. If you rely on host‑based swapping, pin ^1.0.2 and add a regression test that swaps a model and asserts the factory returns the swapped class.
  • Config caching: After changing model mappings in config/<package>.php, run php artisan config:clear (and config:cache in production). Stale config can make factories build the wrong model.
  • Swap ordering: Perform model swaps before resolving factories or calling factory()->create(). If you swap after a factory has already been resolved, the factory may hold a stale model reference.
  • Model contract expectations: The swapped model must satisfy the same interface or base class that the factory expects. Factories typically call create() or fill attributes; ensure the swapped model supports the same mass‑assignment rules and relationships.
  • Debugging resolution: If a factory builds the wrong model, dump the package’s current model map (e.g., Package::getModels() or the config repository) and check whether the swap was applied. Also verify that the factory is using the package’s resolver and not Laravel’s default Factory::guessModelNamesUsing.
  • Test isolation: If you swap models in tests, reset the swap in tearDown() (or use the package’s flushSwaps() if available) to avoid leaking state between tests.
  • Extension points: The package may allow custom factory resolvers via a container binding or config callback. Use these rather than monkey‑patching the factory base class, especially if you need per‑host logic beyond simple class swaps.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
ignibyte/aic-framework
glavweb/data-schema-bundle
glavweb/datagrid-bundle
tacticmedia/scim-server
uni-method/frame
neika-ai/sdk
adachsoft/php-code-style
pharaonic/laravel-notification-dependencies
beautinow/translug
dev-brainiac/bot-cloaked
octoberrain/html
octoberrain/extension
octoberrain/exception
octoberrain/contracts
adachsoft/api-version-bundle
cgv/localized-resource
noith/settings
kda/laravel-package
kda/filament-dynamic-navigation
bypersoft/signbox