majebry / laravel6-userable
Multiple User Type Models for Laravel 6
This package is auto-updated.
Last update: 2024-09-10 16:59:08 UTC
README
This package transforms Laravel's User
model to be an "abstract-like" model so that you can create as much separate userable (user-type) models as you wish with their auth controllers and a middleware by just running some command.
Installation
Use composer dependency manager to install this package.
composer require majebry/laravel6-userable
Usage
- publish migrations:
php artisan vendor:publish --provider="Majebry\LaravelUserable\UserableServiceProvider" --tag="migrations"
- Include Userable trait into your Laravel's User model
class User extends Authenticatable { use \Majebry\LaravelUserable\Traits\Userable; //... }
So, anytime you want to call user info for any userable model you can do Model::find($id)->userable
- Now, for example, if we want to generate a user-type called
Customer
, we run the following command
php artsian userable:make Customer
This command will generate a Customer
model under app
directory
and a x_create_customers_table.php
under database\migrations
directory
You can add the fields that you wish to that migration before running it
- In app/Http/Kernal.php, register the following middleware
protected $routeMiddleware = [ // ... 'userable-auth' => \Majebry\LaravelUserable\Http\Middleware\UserTypeMiddleware::class, ];
Now for example, if you want to protect a route to be only accessible for StoreAdmin
, you can chain the Route with ->middleware('userable-auth:store-admin')
Contributing
...
Todo
- Probably make the generator command also generates custom authentication actions(routes and controllers), middleware and/or views with the generator command.
- Add tests
- Making the UserTypeMiddleware supports multiple user-types
- Hooking into on User/Userable
Delete
event?