Easily manage devices and device tokens for your users.
You can install the package via composer:
composer require sfolador/devices
You can publish the config file with:
php artisan vendor:publish --tag="devices-config"
This is the contents of the published config file:
return [
'allow_device_reassign' => false,
];
if you set allow_device_reassign
to true, it will be possible to
register a device for a user and then assign it to another user. This happens usually
in mobile applications with multi-accounts on the same device.
You can publish and run the migrations with:
php artisan vendor:publish --tag="devices-migrations"
php artisan migrate
the migration will create the Devices
table and its columns will be:
id
- the device idnotifiable_id
- the "user" idnotifiable_type
- the "user" typename
- the device nametype
- the device type (mobile, web)platform
- the device platform (ios, android, web)token
- the device tokencreated_at
- the device creation dateupdated_at
- the device update date
It's possible to use the HasDevices
trait in your User
model:
use Sfolador\Devices\Models\Concerns\HasDevices;
class User extends Authenticatable
{
use HasDevices;
}
At this point is possible to retrieve the devices of a user:
$user = User::find(1);
$user->devices;
To register a new Device
, for example from a mobile app, you can use the provided route POST /api/devices/attach
:
Route::post('/devices/attach', [DeviceController::class, 'attach']);
The register a Device you need a request with these parameters:
$rules = [
'platform' => ['required', new Enum(DevicePlatform::class)],
'type' => ['required', new Enum(DeviceType::class)],
'token' => 'required|string',
];
DevicePlatform is an Enum that can have these values: android, ios, web. DeviceType is an Enum that can have these values: mobile, web
composer test
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.